
clear

use "Data\dataset_with_singles.dta" 

*Keep only necessary variables
drop aegte_nr faelle_nr civst educ_eika final_educ individual grad_region wage_start_mean_ambition wage_growth_ambition wage_start_mean_ambition_s wage_growth_ambition_s married cohab educ_level_field

*Preparation

{
*get 1980 preferences*

*couple matrix

gen ambition=.
replace ambition=1 if ambition_type_k_4_s==1
replace ambition=2 if ambition_type_k_4_s==4
replace ambition=3 if ambition_type_k_4_s==3
replace ambition=4 if ambition_type_k_4_s==2

//generate partners' ambition
by couple_id aar, sort: gen am_male = ambition_type_k_4_s if koen=="1" & relationship==1
by couple_id aar: gen am_female = ambition_type_k_4_s if koen=="2" & relationship==1
by couple_id aar: egen maxeduc = max(am_male)
by couple_id aar: replace am_male = maxeduc
drop maxeduc
by couple_id aar: egen maxeduc = max(am_female)
by couple_id aar: replace am_female = maxeduc
drop maxeduc

//generate partners' educ
by couple_id aar, sort: gen educ_male = fined if koen=="1" & relationship==1
by couple_id aar: gen educ_female = fined if koen=="2" & relationship==1
by couple_id aar: egen maxeduc = max(educ_male)
by couple_id aar: replace educ_male = maxeduc
drop maxeduc
by couple_id aar: egen maxeduc = max(educ_female)
by couple_id aar: replace educ_female = maxeduc
drop maxeduc

//generate partners' field
by couple_id aar, sort: gen field_male = educ_level_field_num if koen=="1" & relationship==1
by couple_id aar: gen field_female = educ_level_field_num if koen=="2" & relationship==1
by couple_id aar: egen maxeduc = max(field_male)
by couple_id aar: replace field_male = maxeduc
drop maxeduc
by couple_id aar: egen maxeduc = max(field_female)
by couple_id aar: replace field_female = maxeduc
drop maxeduc

tab am_male am_female if aar==1980 & koen=="1", matcell(am_base) /*It does not matter if we condition on the husband or wife*/
tab educ_male educ_female if aar==1980 & koen=="1", matcell(educ_base)
tab field_male field_female if aar==1980 & koen=="1", matcell(field_base)

*single men
tab ambition_type_k_4_s if koen=="1" & relationship==0 & aar==1980, matcell(am_single_m_base)
tab fined if koen=="1" & relationship==0 & aar==1980, matcell(educ_single_m_base)
tab educ_level_field_num if koen=="1" & relationship==0 & aar==1980, matcell(field_single_m_base)

*single women
tab ambition_type_k_4_s if koen=="2" & relationship==0 & aar==1980, matcell(temp)
mat am_single_f_base=temp'
tab fined if koen=="2" & relationship==0 & aar==1980, matcell(temp)
mat educ_single_f_base=temp'
tab educ_level_field_num if koen=="2" & relationship==0 & aar==1980, matcell(temp)
mat field_single_f_base=temp'


*get 1980 marginals*
mat am_mar_m_base=J(4,1,.)
forvalues i=1(1)4{
mat am_mar_m_base[`i',1]=am_single_m_base[`i',1]+am_base[`i',1]+am_base[`i',2]+am_base[`i',3]+am_base[`i',4]
}

mat educ_mar_m_base=J(4,1,.)
forvalues i=1(1)4{
mat educ_mar_m_base[`i',1]=educ_single_m_base[`i',1]+educ_base[`i',1]+educ_base[`i',2]+educ_base[`i',3]+educ_base[`i',4]
}

mat field_mar_m_base=J(8,1,.)
forvalues i=1(1)8{
mat field_mar_m_base[`i',1]=field_single_m_base[`i',1]+field_base[`i',1]+field_base[`i',2]+field_base[`i',3]+field_base[`i',4]+field_base[`i',5]+field_base[`i',6]+field_base[`i',7]+field_base[`i',8]
}

mat am_mar_f_base=J(1,4,.)
forvalues i=1(1)4{
mat am_mar_f_base[1,`i']=am_single_f_base[1,`i']+am_base[1,`i']+am_base[2,`i']+am_base[3,`i']+am_base[4,`i']
}

mat educ_mar_f_base=J(1,4,.)
forvalues i=1(1)4{
mat educ_mar_f_base[1,`i']=educ_single_f_base[1,`i']+educ_base[1,`i']+educ_base[2,`i']+educ_base[3,`i']+educ_base[4,`i']
}

mat field_mar_f_base=J(1,8,.)
forvalues i=1(1)8{
mat field_mar_f_base[1,`i']=field_single_f_base[1,`i']+field_base[1,`i']+field_base[2,`i']+field_base[3,`i']+field_base[4,`i']+field_base[5,`i']+field_base[6,`i']+field_base[7,`i']+field_base[8,`i']
}


*preference matrix


*NEW ADDITIONS FOR GUTIERREZ FIX

mat temp=am_base*J(4,1,1)
mat temp=temp+am_single_m_base
mat temp=J(1,4,1)*temp
sca am_N_m=temp[1,1]

mat temp=J(1,4,1)*am_base
mat temp=temp+am_single_f_base
mat temp=temp*J(4,1,1)
sca am_N_f=temp[1,1]


mat am_pi_base=J(4,4,.)
forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat am_pi_base[`i',`j']=(am_base[`i',`j']/((am_single_m_base[`i',1]*am_single_f_base[1,`j'])^(1/2)))*(((1)/(am_mar_m_base[`i',1]*am_mar_f_base[1,`j']))^(1/2))
	}
}



mat temp=educ_base*J(4,1,1)
mat temp=temp+educ_single_m_base
mat temp=J(1,4,1)*temp
sca educ_N_m=temp[1,1]

mat temp=J(1,4,1)*educ_base
mat temp=temp+educ_single_f_base
mat temp=temp*J(4,1,1)
sca educ_N_f=temp[1,1]


mat educ_pi_base=J(4,4,.)
forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat educ_pi_base[`i',`j']=(educ_base[`i',`j']/((educ_single_m_base[`i',1]*educ_single_f_base[1,`j'])^(1/2)))*(((1)/(educ_mar_m_base[`i',1]*educ_mar_f_base[1,`j']))^(1/2))
	}
}

mat temp=field_base*J(8,1,1)
mat temp=temp+field_single_m_base
mat temp=J(1,8,1)*temp
sca field_N_m=temp[1,1]

mat temp=J(1,8,1)*field_base
mat temp=temp+field_single_f_base
mat temp=temp*J(8,1,1)
sca field_N_f=temp[1,1]



mat field_pi_base=J(8,8,.)
forvalues i=1(1)8{
	forvalues j=1(1)8{
		mat field_pi_base[`i',`j']=(field_base[`i',`j']/((field_single_m_base[`i',1]*field_single_f_base[1,`j'])^(1/2)))*(((1)/(field_mar_m_base[`i',1]*field_mar_f_base[1,`j']))^(1/2))
	}
}



*get later preferences*

forvalues y=1981(1)2018{
tab am_male am_female if aar==`y' & koen=="1", matcell(am_`y') /*It does not matter if we condition on the husband or wife*/
tab educ_male educ_female if aar==`y' & koen=="1", matcell(educ_`y')
tab field_male field_female if aar==`y' & koen=="1", matcell(field_`y')

*single men
tab ambition_type_k_4_s if koen=="1" & relationship==0 & aar==`y', matcell(am_single_m_`y')
tab fined if koen=="1" & relationship==0 & aar==`y', matcell(educ_single_m_`y')
tab educ_level_field_num if koen=="1" & relationship==0 & aar==`y', matcell(field_single_m_`y')

*single women
tab ambition_type_k_4_s if koen=="2" & relationship==0 & aar==`y', matcell(temp)
mat am_single_f_`y'=temp'
tab fined if koen=="2" & relationship==0 & aar==`y', matcell(temp)
mat educ_single_f_`y'=temp'
tab educ_level_field_num if koen=="2" & relationship==0 & aar==`y', matcell(temp)
mat field_single_f_`y'=temp'




*get later marginals*
mat am_mar_m_`y'=J(4,1,.)
forvalues i=1(1)4{
mat am_mar_m_`y'[`i',1]=am_single_m_`y'[`i',1]+am_`y'[`i',1]+am_`y'[`i',2]+am_`y'[`i',3]+am_`y'[`i',4]
}

mat educ_mar_m_`y'=J(4,1,.)
forvalues i=1(1)4{
mat educ_mar_m_`y'[`i',1]=educ_single_m_`y'[`i',1]+educ_`y'[`i',1]+educ_`y'[`i',2]+educ_`y'[`i',3]+educ_`y'[`i',4]
}

mat field_mar_m_`y'=J(8,1,.)
forvalues i=1(1)8{
mat field_mar_m_`y'[`i',1]=field_single_m_`y'[`i',1]+field_`y'[`i',1]+field_`y'[`i',2]+field_`y'[`i',3]+field_`y'[`i',4]+field_`y'[`i',5]+field_`y'[`i',6]+field_`y'[`i',7]+field_`y'[`i',8]
}

mat am_mar_f_`y'=J(1,4,.)
forvalues i=1(1)4{
mat am_mar_f_`y'[1,`i']=am_single_f_`y'[1,`i']+am_`y'[1,`i']+am_`y'[2,`i']+am_`y'[3,`i']+am_`y'[4,`i']
}

mat educ_mar_f_`y'=J(1,4,.)
forvalues i=1(1)4{
mat educ_mar_f_`y'[1,`i']=educ_single_f_`y'[1,`i']+educ_`y'[1,`i']+educ_`y'[2,`i']+educ_`y'[3,`i']+educ_`y'[4,`i']
}

mat field_mar_f_`y'=J(1,8,.)
forvalues i=1(1)8{
mat field_mar_f_`y'[1,`i']=field_single_f_`y'[1,`i']+field_`y'[1,`i']+field_`y'[2,`i']+field_`y'[3,`i']+field_`y'[4,`i']+field_`y'[5,`i']+field_`y'[6,`i']+field_`y'[7,`i']+field_`y'[8,`i']
}


*preference matrix

mat temp=am_`y'*J(4,1,1)
mat temp=temp+am_single_m_`y'
mat temp=J(1,4,1)*temp
sca am_N_`y'_m=temp[1,1]

mat temp=J(1,4,1)*am_`y'
mat temp=temp+am_single_f_`y'
mat temp=temp*J(4,1,1)
sca am_N_`y'_f=temp[1,1]


mat am_pi_`y'=J(4,4,.)
forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat am_pi_`y'[`i',`j']=(am_`y'[`i',`j']/((am_single_m_`y'[`i',1]*am_single_f_`y'[1,`j'])^(1/2)))*(((1)/(am_mar_m_`y'[`i',1]*am_mar_f_`y'[1,`j']))^(1/2))
	}
}

mat temp=educ_`y'*J(4,1,1)
mat temp=temp+educ_single_m_`y'
mat temp=J(1,4,1)*temp
sca educ_N_`y'_m=temp[1,1]

mat temp=J(1,4,1)*educ_`y'
mat temp=temp+educ_single_f_`y'
mat temp=temp*J(4,1,1)
sca educ_N_`y'_f=temp[1,1]


mat educ_pi_`y'=J(4,4,.)
forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat educ_pi_`y'[`i',`j']=(educ_`y'[`i',`j']/((educ_single_m_`y'[`i',1]*educ_single_f_`y'[1,`j'])^(1/2)))*(((1)/(educ_mar_m_`y'[`i',1]*educ_mar_f_`y'[1,`j']))^(1/2))
	}
}


mat temp=field_`y'*J(8,1,1)
mat temp=temp+field_single_m_`y'
mat temp=J(1,8,1)*temp
sca field_N_`y'_m=temp[1,1]

mat temp=J(1,8,1)*field_`y'
mat temp=temp+field_single_f_`y'
mat temp=temp*J(8,1,1)
sca field_N_`y'_f=temp[1,1]


mat field_pi_`y'=J(8,8,.)
forvalues i=1(1)8{
	forvalues j=1(1)8{
		mat field_pi_`y'[`i',`j']=(field_`y'[`i',`j']/((field_single_m_`y'[`i',1]*field_single_f_`y'[1,`j'])^(1/2)))*(((1)/(field_mar_m_`y'[`i',1]*field_mar_f_`y'[1,`j']))^(1/2))
	}
}
}

}

**Inequality**
gen couple_id_with_s=couple_id
replace couple_id_with_s=pnr if relationship==0
sort couple_id_with_s aar koen

gen individual_earnings=erhvervsindk_13
replace individual_earnings=0 if individual_earnings<0

by couple_id_with_s aar: egen household_earnings=sum(individual_earnings)

**ADDITION**
replace household_earnings=household_earnings/1.5 if relationship==1
**

keep if (koen=="1" & relationship==1) | relationship==0 

*Keep only necessary variables
drop erhvervsindk_13 couple_id couple_id_with_s individual_earnings

**Data**
{

*Data with singles* /*Takes between 3-5 hours to run*/
forvalues y=1980(1)2018{
qui ineqdeco household_earnings if aar==`y'

sca gini_`y'=r(gini)
sca p90p50_`y'=r(p90p50)
sca p50p10_`y'=r(p50)/r(p10)
}

frame copy default results
frame change results
gen temp=.
collapse (first) temp, by(aar)
drop temp

gen gini=.
forvalues i=1980(1)2018{
replace gini=gini_`i' if aar==`i' 
}

gen p90p50=.
forvalues i=1980(1)2018{
replace p90p50=p90p50_`i' if aar==`i' 
}

gen p50p10=.
forvalues i=1980(1)2018{
replace p50p10=p50p10_`i' if aar==`i' 
}

frame change default



}

*Fixed marital surplus*
{
*Solve for year 2018 single numbers with 1980 preferences*

*ambition types

program nlfaq

syntax varlist(min=1 max=1) [if], at(name)

tempname m1 m2 m3 m4 f1 f2 f3 f4

scalar `m1' = `at'[1,1]
scalar `m2' = `at'[1,2]
scalar `m3' = `at'[1,3]
scalar `m4' = `at'[1,4]
scalar `f1' = `at'[1,5]
scalar `f2' = `at'[1,6]
scalar `f3' = `at'[1,7]
scalar `f4' = `at'[1,8]

tempvar yh
gen double `yh'= am_pi_base[1,1]*((`m1'*`f1')^(1/2))*((am_mar_m_2018[1,1]*am_mar_f_2018[1,1])^((1/2)))+am_pi_base[1,2]*((`m1'*`f2')^(1/2))*(((am_mar_m_2018[1,1]*am_mar_f_2018[1,2]))^((1/2)))+am_pi_base[1,3]*((`m1'*`f3')^(1/2))*(((am_mar_m_2018[1,1]*am_mar_f_2018[1,3]))^((1/2)))+am_pi_base[1,4]*((`m1'*`f4')^(1/2))*(((am_mar_m_2018[1,1]*am_mar_f_2018[1,4]))^((1/2)))+`m1'-am_mar_m_2018[1,1]+1 in 1

replace `yh'= am_pi_base[2,1]*((`m2'*`f1')^(1/2))*(((am_mar_m_2018[2,1]*am_mar_f_2018[1,1]))^((1/2)))+am_pi_base[2,2]*((`m2'*`f2')^(1/2))*(((am_mar_m_2018[2,1]*am_mar_f_2018[1,2]))^((1/2)))+am_pi_base[2,3]*((`m2'*`f3')^(1/2))*(((am_mar_m_2018[2,1]*am_mar_f_2018[1,3]))^((1/2)))+am_pi_base[2,4]*((`m2'*`f4')^(1/2))*(((am_mar_m_2018[2,1]*am_mar_f_2018[1,4]))^((1/2)))+`m2'-am_mar_m_2018[2,1]+1 in 2

replace `yh'= am_pi_base[3,1]*((`m3'*`f1')^(1/2))*(((am_mar_m_2018[3,1]*am_mar_f_2018[1,1]))^((1/2)))+am_pi_base[3,2]*((`m3'*`f2')^(1/2))*(((am_mar_m_2018[3,1]*am_mar_f_2018[1,2]))^((1/2)))+am_pi_base[3,3]*((`m3'*`f3')^(1/2))*(((am_mar_m_2018[3,1]*am_mar_f_2018[1,3]))^((1/2)))+am_pi_base[3,4]*((`m3'*`f4')^(1/2))*(((am_mar_m_2018[3,1]*am_mar_f_2018[1,4]))^((1/2)))+`m3'-am_mar_m_2018[3,1]+1 in 3

replace `yh'= am_pi_base[4,1]*((`m4'*`f1')^(1/2))*(((am_mar_m_2018[4,1]*am_mar_f_2018[1,1]))^((1/2)))+am_pi_base[4,2]*((`m4'*`f2')^(1/2))*(((am_mar_m_2018[4,1]*am_mar_f_2018[1,2]))^((1/2)))+am_pi_base[4,3]*((`m4'*`f3')^(1/2))*(((am_mar_m_2018[4,1]*am_mar_f_2018[1,3]))^((1/2)))+am_pi_base[4,4]*((`m4'*`f4')^(1/2))*(((am_mar_m_2018[4,1]*am_mar_f_2018[1,4]))^((1/2)))+`m4'-am_mar_m_2018[4,1]+1 in 4

replace `yh'=am_pi_base[1,1]*((`m1'*`f1')^(1/2))*(((am_mar_m_2018[1,1]*am_mar_f_2018[1,1]))^((1/2)))+am_pi_base[2,1]*((`m2'*`f1')^(1/2))*(((am_mar_m_2018[2,1]*am_mar_f_2018[1,1]))^((1/2)))+am_pi_base[3,1]*((`m3'*`f1')^(1/2))*(((am_mar_m_2018[3,1]*am_mar_f_2018[1,1]))^((1/2)))+am_pi_base[4,1]*((`m4'*`f1')^(1/2))*(((am_mar_m_2018[4,1]*am_mar_f_2018[1,1]))^((1/2)))+`f1'-am_mar_f_2018[1,1] in 5

replace `yh'=am_pi_base[1,2]*((`m1'*`f2')^(1/2))*(((am_mar_m_2018[1,1]*am_mar_f_2018[1,2]))^((1/2)))+am_pi_base[2,2]*((`m2'*`f2')^(1/2))*(((am_mar_m_2018[2,1]*am_mar_f_2018[1,2]))^((1/2)))+am_pi_base[3,2]*((`m3'*`f2')^(1/2))*(((am_mar_m_2018[3,1]*am_mar_f_2018[1,2]))^((1/2)))+am_pi_base[4,2]*((`m4'*`f2')^(1/2))*(((am_mar_m_2018[4,1]*am_mar_f_2018[1,2]))^((1/2)))+`f2'-am_mar_f_2018[1,2] in 6

replace `yh'=am_pi_base[1,3]*((`m1'*`f3')^(1/2))*(((am_mar_m_2018[1,1]*am_mar_f_2018[1,3]))^((1/2)))+am_pi_base[2,3]*((`m2'*`f3')^(1/2))*(((am_mar_m_2018[2,1]*am_mar_f_2018[1,3]))^((1/2)))+am_pi_base[3,3]*((`m3'*`f3')^(1/2))*(((am_mar_m_2018[3,1]*am_mar_f_2018[1,3]))^((1/2)))+am_pi_base[4,3]*((`m4'*`f3')^(1/2))*(((am_mar_m_2018[4,1]*am_mar_f_2018[1,3]))^((1/2)))+`f3'-am_mar_f_2018[1,3] in 7

replace `yh'=am_pi_base[1,4]*((`m1'*`f4')^(1/2))*(((am_mar_m_2018[1,1]*am_mar_f_2018[1,4]))^((1/2)))+am_pi_base[2,4]*((`m2'*`f4')^(1/2))*(((am_mar_m_2018[2,1]*am_mar_f_2018[1,4]))^((1/2)))+am_pi_base[3,4]*((`m3'*`f4')^(1/2))*(((am_mar_m_2018[3,1]*am_mar_f_2018[1,4]))^((1/2)))+am_pi_base[4,4]*((`m4'*`f4')^(1/2))*(((am_mar_m_2018[4,1]*am_mar_f_2018[1,4]))^((1/2)))+`f4'-am_mar_f_2018[1,4] in 8



replace `varlist'=`yh'
end


frame create temp
frame change temp

clear
set obs 8

gen y = 0
replace y=1 in 1

nl faq @ y, parameters(m1 m2 m3 m4 f1 f2 f3 f4) initial(m1 1 m2 1 m3 1 m4 1 f1 1 f2 1 f3 1 f4 1)

*get counter factual singles
mat am_single_m_cf_pref_2018=J(4,1,.)
mat am_single_m_cf_pref_2018[1,1]=[m1]_b[_cons]
mat am_single_m_cf_pref_2018[2,1]=[m2]_b[_cons]
mat am_single_m_cf_pref_2018[3,1]=[m3]_b[_cons]
mat am_single_m_cf_pref_2018[4,1]=[m4]_b[_cons]

mat am_single_f_cf_pref_2018=J(1,4,.)
mat am_single_f_cf_pref_2018[1,1]=[f1]_b[_cons]
mat am_single_f_cf_pref_2018[1,2]=[f2]_b[_cons]
mat am_single_f_cf_pref_2018[1,3]=[f3]_b[_cons]
mat am_single_f_cf_pref_2018[1,4]=[f4]_b[_cons]

frame change default
frame drop temp
prog drop nlfaq




*education types
program nlfaq

syntax varlist(min=1 max=1) [if], at(name)

tempname m1 m2 m3 m4 f1 f2 f3 f4

scalar `m1' = `at'[1,1]
scalar `m2' = `at'[1,2]
scalar `m3' = `at'[1,3]
scalar `m4' = `at'[1,4]
scalar `f1' = `at'[1,5]
scalar `f2' = `at'[1,6]
scalar `f3' = `at'[1,7]
scalar `f4' = `at'[1,8]

tempvar yh
gen double `yh'= educ_pi_base[1,1]*((`m1'*`f1')^(1/2))*((educ_mar_m_2018[1,1]*educ_mar_f_2018[1,1])^((1/2)))+educ_pi_base[1,2]*((`m1'*`f2')^(1/2))*(((educ_mar_m_2018[1,1]*educ_mar_f_2018[1,2]))^((1/2)))+educ_pi_base[1,3]*((`m1'*`f3')^(1/2))*(((educ_mar_m_2018[1,1]*educ_mar_f_2018[1,3]))^((1/2)))+educ_pi_base[1,4]*((`m1'*`f4')^(1/2))*(((educ_mar_m_2018[1,1]*educ_mar_f_2018[1,4]))^((1/2)))+`m1'-educ_mar_m_2018[1,1]+1 in 1

replace `yh'= educ_pi_base[2,1]*((`m2'*`f1')^(1/2))*(((educ_mar_m_2018[2,1]*educ_mar_f_2018[1,1]))^((1/2)))+educ_pi_base[2,2]*((`m2'*`f2')^(1/2))*(((educ_mar_m_2018[2,1]*educ_mar_f_2018[1,2]))^((1/2)))+educ_pi_base[2,3]*((`m2'*`f3')^(1/2))*(((educ_mar_m_2018[2,1]*educ_mar_f_2018[1,3]))^((1/2)))+educ_pi_base[2,4]*((`m2'*`f4')^(1/2))*(((educ_mar_m_2018[2,1]*educ_mar_f_2018[1,4]))^((1/2)))+`m2'-educ_mar_m_2018[2,1]+1 in 2

replace `yh'= educ_pi_base[3,1]*((`m3'*`f1')^(1/2))*(((educ_mar_m_2018[3,1]*educ_mar_f_2018[1,1]))^((1/2)))+educ_pi_base[3,2]*((`m3'*`f2')^(1/2))*(((educ_mar_m_2018[3,1]*educ_mar_f_2018[1,2]))^((1/2)))+educ_pi_base[3,3]*((`m3'*`f3')^(1/2))*(((educ_mar_m_2018[3,1]*educ_mar_f_2018[1,3]))^((1/2)))+educ_pi_base[3,4]*((`m3'*`f4')^(1/2))*(((educ_mar_m_2018[3,1]*educ_mar_f_2018[1,4]))^((1/2)))+`m3'-educ_mar_m_2018[3,1]+1 in 3

replace `yh'= educ_pi_base[4,1]*((`m4'*`f1')^(1/2))*(((educ_mar_m_2018[4,1]*educ_mar_f_2018[1,1]))^((1/2)))+educ_pi_base[4,2]*((`m4'*`f2')^(1/2))*(((educ_mar_m_2018[4,1]*educ_mar_f_2018[1,2]))^((1/2)))+educ_pi_base[4,3]*((`m4'*`f3')^(1/2))*(((educ_mar_m_2018[4,1]*educ_mar_f_2018[1,3]))^((1/2)))+educ_pi_base[4,4]*((`m4'*`f4')^(1/2))*(((educ_mar_m_2018[4,1]*educ_mar_f_2018[1,4]))^((1/2)))+`m4'-educ_mar_m_2018[4,1]+1 in 4

replace `yh'=educ_pi_base[1,1]*((`m1'*`f1')^(1/2))*(((educ_mar_m_2018[1,1]*educ_mar_f_2018[1,1]))^((1/2)))+educ_pi_base[2,1]*((`m2'*`f1')^(1/2))*(((educ_mar_m_2018[2,1]*educ_mar_f_2018[1,1]))^((1/2)))+educ_pi_base[3,1]*((`m3'*`f1')^(1/2))*(((educ_mar_m_2018[3,1]*educ_mar_f_2018[1,1]))^((1/2)))+educ_pi_base[4,1]*((`m4'*`f1')^(1/2))*(((educ_mar_m_2018[4,1]*educ_mar_f_2018[1,1]))^((1/2)))+`f1'-educ_mar_f_2018[1,1] in 5

replace `yh'=educ_pi_base[1,2]*((`m1'*`f2')^(1/2))*(((educ_mar_m_2018[1,1]*educ_mar_f_2018[1,2]))^((1/2)))+educ_pi_base[2,2]*((`m2'*`f2')^(1/2))*(((educ_mar_m_2018[2,1]*educ_mar_f_2018[1,2]))^((1/2)))+educ_pi_base[3,2]*((`m3'*`f2')^(1/2))*(((educ_mar_m_2018[3,1]*educ_mar_f_2018[1,2]))^((1/2)))+educ_pi_base[4,2]*((`m4'*`f2')^(1/2))*(((educ_mar_m_2018[4,1]*educ_mar_f_2018[1,2]))^((1/2)))+`f2'-educ_mar_f_2018[1,2] in 6

replace `yh'=educ_pi_base[1,3]*((`m1'*`f3')^(1/2))*(((educ_mar_m_2018[1,1]*educ_mar_f_2018[1,3]))^((1/2)))+educ_pi_base[2,3]*((`m2'*`f3')^(1/2))*(((educ_mar_m_2018[2,1]*educ_mar_f_2018[1,3]))^((1/2)))+educ_pi_base[3,3]*((`m3'*`f3')^(1/2))*(((educ_mar_m_2018[3,1]*educ_mar_f_2018[1,3]))^((1/2)))+educ_pi_base[4,3]*((`m4'*`f3')^(1/2))*(((educ_mar_m_2018[4,1]*educ_mar_f_2018[1,3]))^((1/2)))+`f3'-educ_mar_f_2018[1,3] in 7

replace `yh'=educ_pi_base[1,4]*((`m1'*`f4')^(1/2))*(((educ_mar_m_2018[1,1]*educ_mar_f_2018[1,4]))^((1/2)))+educ_pi_base[2,4]*((`m2'*`f4')^(1/2))*(((educ_mar_m_2018[2,1]*educ_mar_f_2018[1,4]))^((1/2)))+educ_pi_base[3,4]*((`m3'*`f4')^(1/2))*(((educ_mar_m_2018[3,1]*educ_mar_f_2018[1,4]))^((1/2)))+educ_pi_base[4,4]*((`m4'*`f4')^(1/2))*(((educ_mar_m_2018[4,1]*educ_mar_f_2018[1,4]))^((1/2)))+`f4'-educ_mar_f_2018[1,4] in 8



replace `varlist'=`yh'
end


frame create temp
frame change temp

clear
set obs 8

gen y = 0
replace y=1 in 1

nl faq @ y, parameters(m1 m2 m3 m4 f1 f2 f3 f4) initial(m1 1 m2 1 m3 1 m4 1 f1 1 f2 1 f3 1 f4 1)

*get counter factual singles
mat educ_single_m_cf_pref_2018=J(4,1,.)
mat educ_single_m_cf_pref_2018[1,1]=[m1]_b[_cons]
mat educ_single_m_cf_pref_2018[2,1]=[m2]_b[_cons]
mat educ_single_m_cf_pref_2018[3,1]=[m3]_b[_cons]
mat educ_single_m_cf_pref_2018[4,1]=[m4]_b[_cons]

mat educ_single_f_cf_pref_2018=J(1,4,.)
mat educ_single_f_cf_pref_2018[1,1]=[f1]_b[_cons]
mat educ_single_f_cf_pref_2018[1,2]=[f2]_b[_cons]
mat educ_single_f_cf_pref_2018[1,3]=[f3]_b[_cons]
mat educ_single_f_cf_pref_2018[1,4]=[f4]_b[_cons]

frame change default
frame drop temp
prog drop nlfaq

*field types
program nlfaq

syntax varlist(min=1 max=1) [if], at(name)

tempname m1 m2 m3 m4 m5 m6 m7 m8 f1 f2 f3 f4 f5 f6 f7 f8

scalar `m1' = `at'[1,1]
scalar `m2' = `at'[1,2]
scalar `m3' = `at'[1,3]
scalar `m4' = `at'[1,4]
scalar `m5' = `at'[1,5]
scalar `m6' = `at'[1,6]
scalar `m7' = `at'[1,7]
scalar `m8' = `at'[1,8]
scalar `f1' = `at'[1,9]
scalar `f2' = `at'[1,10]
scalar `f3' = `at'[1,11]
scalar `f4' = `at'[1,12]
scalar `f5' = `at'[1,13]
scalar `f6' = `at'[1,14]
scalar `f7' = `at'[1,15]
scalar `f8' = `at'[1,16]

tempvar yh
gen double `yh'= field_pi_base[1,1]*((`m1'*`f1')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_base[1,2]*((`m1'*`f2')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_base[1,3]*((`m1'*`f3')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_base[1,4]*((`m1'*`f4')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_base[1,5]*((`m1'*`f5')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_base[1,6]*((`m1'*`f6')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_base[1,7]*((`m1'*`f7')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_base[1,8]*((`m1'*`f8')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_2018[1,8]))^((1/2)))+`m1'-field_mar_m_2018[1,1]+1 in 1

replace `yh'= field_pi_base[2,1]*((`m2'*`f1')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_base[2,2]*((`m2'*`f2')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_base[2,3]*((`m2'*`f3')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_base[2,4]*((`m2'*`f4')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_base[2,5]*((`m2'*`f5')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_base[2,6]*((`m2'*`f6')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_base[2,7]*((`m2'*`f7')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_base[2,8]*((`m2'*`f8')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_2018[1,8]))^((1/2)))+`m2'-field_mar_m_2018[2,1]+1 in 2

replace `yh'= field_pi_base[3,1]*((`m3'*`f1')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_base[3,2]*((`m3'*`f2')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_base[3,3]*((`m3'*`f3')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_base[3,4]*((`m3'*`f4')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_base[3,5]*((`m3'*`f5')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_base[3,6]*((`m3'*`f6')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_base[3,7]*((`m3'*`f7')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_base[3,8]*((`m3'*`f8')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_2018[1,8]))^((1/2)))+`m3'-field_mar_m_2018[3,1]+1 in 3

replace `yh'= field_pi_base[4,1]*((`m4'*`f1')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_base[4,2]*((`m4'*`f2')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_base[4,3]*((`m4'*`f3')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_base[4,4]*((`m4'*`f4')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_base[4,5]*((`m4'*`f5')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_base[4,6]*((`m4'*`f6')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_base[4,7]*((`m4'*`f7')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_base[4,8]*((`m4'*`f8')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_2018[1,8]))^((1/2)))+`m4'-field_mar_m_2018[4,1]+1 in 4

replace `yh'= field_pi_base[5,1]*((`m5'*`f1')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_base[5,2]*((`m5'*`f2')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_base[5,3]*((`m5'*`f3')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_base[5,4]*((`m5'*`f4')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_base[5,5]*((`m5'*`f5')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_base[5,6]*((`m5'*`f6')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_base[5,7]*((`m5'*`f7')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_base[5,8]*((`m5'*`f8')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_2018[1,8]))^((1/2)))+`m5'-field_mar_m_2018[5,1]+1 in 5

replace `yh'= field_pi_base[6,1]*((`m6'*`f1')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_base[6,2]*((`m6'*`f2')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_base[6,3]*((`m6'*`f3')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_base[6,4]*((`m6'*`f4')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_base[6,5]*((`m6'*`f5')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_base[6,6]*((`m6'*`f6')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_base[6,7]*((`m6'*`f7')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_base[6,8]*((`m6'*`f8')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_2018[1,8]))^((1/2)))+`m6'-field_mar_m_2018[6,1]+1 in 6

replace `yh'= field_pi_base[7,1]*((`m7'*`f1')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_base[7,2]*((`m7'*`f2')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_base[7,3]*((`m7'*`f3')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_base[7,4]*((`m7'*`f4')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_base[7,5]*((`m7'*`f5')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_base[7,6]*((`m7'*`f6')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_base[7,7]*((`m7'*`f7')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_base[7,8]*((`m7'*`f8')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_2018[1,8]))^((1/2)))+`m7'-field_mar_m_2018[7,1]+1 in 7

replace `yh'= field_pi_base[8,1]*((`m8'*`f1')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_base[8,2]*((`m8'*`f2')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_base[8,3]*((`m8'*`f3')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_base[8,4]*((`m8'*`f4')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_base[8,5]*((`m8'*`f5')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_base[8,6]*((`m8'*`f6')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_base[8,7]*((`m8'*`f7')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_base[8,8]*((`m8'*`f8')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_2018[1,8]))^((1/2)))+`m8'-field_mar_m_2018[8,1]+1 in 8

replace `yh'=field_pi_base[1,1]*((`m1'*`f1')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_base[2,1]*((`m2'*`f1')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_base[3,1]*((`m3'*`f1')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_base[4,1]*((`m4'*`f1')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_base[5,1]*((`m5'*`f1')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_base[6,1]*((`m6'*`f1')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_base[7,1]*((`m7'*`f1')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_base[8,1]*((`m8'*`f1')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_2018[1,1]))^((1/2)))+`f1'-field_mar_f_2018[1,1] in 9

replace `yh'=field_pi_base[1,2]*((`m1'*`f2')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_base[2,2]*((`m2'*`f2')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_base[3,2]*((`m3'*`f2')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_base[4,2]*((`m4'*`f2')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_base[5,2]*((`m5'*`f2')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_base[6,2]*((`m6'*`f2')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_base[7,2]*((`m7'*`f2')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_base[8,2]*((`m8'*`f2')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_2018[1,2]))^((1/2)))+`f2'-field_mar_f_2018[1,2] in 10

replace `yh'=field_pi_base[1,3]*((`m1'*`f3')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_base[2,3]*((`m2'*`f3')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_base[3,3]*((`m3'*`f3')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_base[4,3]*((`m4'*`f3')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_base[5,3]*((`m5'*`f3')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_base[6,3]*((`m6'*`f3')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_base[7,3]*((`m7'*`f3')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_base[8,3]*((`m8'*`f3')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_2018[1,3]))^((1/2)))+`f3'-field_mar_f_2018[1,3] in 11

replace `yh'=field_pi_base[1,4]*((`m1'*`f4')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_base[2,4]*((`m2'*`f4')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_base[3,4]*((`m3'*`f4')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_base[4,4]*((`m4'*`f4')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_base[5,4]*((`m5'*`f4')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_base[6,4]*((`m6'*`f4')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_base[7,4]*((`m7'*`f4')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_base[8,4]*((`m8'*`f4')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_2018[1,4]))^((1/2)))+`f4'-field_mar_f_2018[1,4] in 12

replace `yh'=field_pi_base[1,5]*((`m1'*`f5')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_base[2,5]*((`m2'*`f5')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_base[3,5]*((`m3'*`f5')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_base[4,5]*((`m4'*`f5')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_base[5,5]*((`m5'*`f5')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_base[6,5]*((`m6'*`f5')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_base[7,5]*((`m7'*`f5')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_base[8,5]*((`m8'*`f5')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_2018[1,5]))^((1/2)))+`f5'-field_mar_f_2018[1,5] in 13

replace `yh'=field_pi_base[1,6]*((`m1'*`f6')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_base[2,6]*((`m2'*`f6')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_base[3,6]*((`m3'*`f6')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_base[4,6]*((`m4'*`f6')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_base[5,6]*((`m5'*`f6')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_base[6,6]*((`m6'*`f6')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_base[7,6]*((`m7'*`f6')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_base[8,6]*((`m8'*`f6')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_2018[1,6]))^((1/2)))+`f6'-field_mar_f_2018[1,6] in 14

replace `yh'=field_pi_base[1,7]*((`m1'*`f7')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_base[2,7]*((`m2'*`f7')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_base[3,7]*((`m3'*`f7')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_base[4,7]*((`m4'*`f7')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_base[5,7]*((`m5'*`f7')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_base[6,7]*((`m6'*`f7')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_base[7,7]*((`m7'*`f7')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_base[8,7]*((`m8'*`f7')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_2018[1,7]))^((1/2)))+`f7'-field_mar_f_2018[1,7] in 15

replace `yh'=field_pi_base[1,8]*((`m1'*`f8')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_2018[1,8]))^((1/2)))+field_pi_base[2,8]*((`m2'*`f8')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_2018[1,8]))^((1/2)))+field_pi_base[3,8]*((`m3'*`f8')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_2018[1,8]))^((1/2)))+field_pi_base[4,8]*((`m4'*`f8')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_2018[1,8]))^((1/2)))+field_pi_base[5,8]*((`m5'*`f8')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_2018[1,8]))^((1/2)))+field_pi_base[6,8]*((`m6'*`f8')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_2018[1,8]))^((1/2)))+field_pi_base[7,8]*((`m7'*`f8')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_2018[1,8]))^((1/2)))+field_pi_base[8,8]*((`m8'*`f8')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_2018[1,8]))^((1/2)))+`f8'-field_mar_f_2018[1,8] in 16



replace `varlist'=`yh'
end


frame create temp
frame change temp

clear
set obs 16

gen y = 0
replace y=1 in 1

nl faq @ y, parameters(m1 m2 m3 m4 m5 m6 m7 m8 f1 f2 f3 f4 f5 f6 f7 f8) initial(m1 1 m2 1 m3 1 m4 1 m5 1 m6 1 m7 1 m8 1 f1 1 f2 1 f3 1 f4 1 f5 1 f6 1 f7 1 f8 1)

*get counter factual singles
mat field_single_m_cf_pref_2018=J(8,1,.)
mat field_single_m_cf_pref_2018[1,1]=[m1]_b[_cons]
mat field_single_m_cf_pref_2018[2,1]=[m2]_b[_cons]
mat field_single_m_cf_pref_2018[3,1]=[m3]_b[_cons]
mat field_single_m_cf_pref_2018[4,1]=[m4]_b[_cons]
mat field_single_m_cf_pref_2018[5,1]=[m5]_b[_cons]
mat field_single_m_cf_pref_2018[6,1]=[m6]_b[_cons]
mat field_single_m_cf_pref_2018[7,1]=[m7]_b[_cons]
mat field_single_m_cf_pref_2018[8,1]=[m8]_b[_cons]

mat field_single_f_cf_pref_2018=J(1,8,.)
mat field_single_f_cf_pref_2018[1,1]=[f1]_b[_cons]
mat field_single_f_cf_pref_2018[1,2]=[f2]_b[_cons]
mat field_single_f_cf_pref_2018[1,3]=[f3]_b[_cons]
mat field_single_f_cf_pref_2018[1,4]=[f4]_b[_cons]
mat field_single_f_cf_pref_2018[1,5]=[f5]_b[_cons]
mat field_single_f_cf_pref_2018[1,6]=[f6]_b[_cons]
mat field_single_f_cf_pref_2018[1,7]=[f7]_b[_cons]
mat field_single_f_cf_pref_2018[1,8]=[f8]_b[_cons]


frame change default
frame drop temp
prog drop nlfaq

*get counter factual couple numbers*

mat am_cf_pref_2018=J(4,4,.)

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat am_cf_pref_2018[`i',`j']=am_pi_base[`i',`j']*((am_single_m_cf_pref_2018[`i',1]*am_single_f_cf_pref_2018[1,`j'])^(1/2))*((am_mar_m_2018[`i',1]*am_mar_f_2018[1,`j'])^((1/2)))
	}
}

mat educ_cf_pref_2018=J(4,4,.)

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat educ_cf_pref_2018[`i',`j']=educ_pi_base[`i',`j']*((educ_single_m_cf_pref_2018[`i',1]*educ_single_f_cf_pref_2018[1,`j'])^(1/2))*((educ_mar_m_2018[`i',1]*educ_mar_f_2018[1,`j'])^((1/2)))
	}
}

mat field_cf_pref_2018=J(8,8,.)

forvalues i=1(1)8{
	forvalues j=1(1)8{
		mat field_cf_pref_2018[`i',`j']=field_pi_base[`i',`j']*((field_single_m_cf_pref_2018[`i',1]*field_single_f_cf_pref_2018[1,`j'])^(1/2))*((field_mar_m_2018[`i',1]*field_mar_f_2018[1,`j'])^((1/2)))
	}
}


*obtain weights - ambition

*numerator 1
mat am_num_1_couples=J(4,4,.) /*The prob. of counterfactual given couple cell x,x*/

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat am_num_1_couples[`i',`j']=am_cf_pref_2018[`i',`j']/(am_cf_pref_2018[`i',`j']+am_2018[`i',`j'])
	}
}

mat am_num_1_single_m=J(4,1,.) /*The prob. of counterfactual given single men cell x*/

forvalues i=1(1)4{
	mat am_num_1_single_m[`i',1]=am_single_m_cf_pref_2018[`i',1]/(am_single_m_cf_pref_2018[`i',1]+am_single_m_2018[`i',1])
}

mat am_num_1_single_f=J(1,4,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)4{
	mat am_num_1_single_f[1,`i']=am_single_f_cf_pref_2018[1,`i']/(am_single_f_cf_pref_2018[1,`i']+am_single_f_2018[1,`i'])
}

*denominator 1
mat am_de_1_couples=J(4,4,.) /*The prob. of factual given couple cell x,x*/

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat am_de_1_couples[`i',`j']=am_2018[`i',`j']/(am_cf_pref_2018[`i',`j']+am_2018[`i',`j'])
	}
}

mat am_de_1_single_m=J(4,1,.) /*The prob. of factual given single men cell x*/

forvalues i=1(1)4{
	mat am_de_1_single_m[`i',1]=am_single_m_2018[`i',1]/(am_single_m_cf_pref_2018[`i',1]+am_single_m_2018[`i',1])
}

mat am_de_1_single_f=J(1,4,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)4{
	mat am_de_1_single_f[1,`i']=am_single_f_2018[1,`i']/(am_single_f_cf_pref_2018[1,`i']+am_single_f_2018[1,`i'])
}

*numerator 2 /*The prob. of being in factual sample*/

sca number_factual=am_2018[1,1]+am_2018[1,2]+am_2018[1,3]+am_2018[1,4] ///
	+am_2018[2,1]+am_2018[2,2]+am_2018[2,3]+am_2018[2,4] ///
	+am_2018[3,1]+am_2018[3,2]+am_2018[3,3]+am_2018[3,4] ///
	+am_2018[4,1]+am_2018[4,2]+am_2018[4,3]+am_2018[4,4] ///
	+am_single_m_2018[1,1]+am_single_m_2018[2,1]+am_single_m_2018[3,1]+am_single_m_2018[4,1] ///
	+am_single_f_2018[1,1]+am_single_f_2018[1,2]+am_single_f_2018[1,3]+am_single_f_2018[1,4]
	
sca number_counterfactual=am_cf_pref_2018[1,1]+am_cf_pref_2018[1,2]+am_cf_pref_2018[1,3]+am_cf_pref_2018[1,4] ///
	+am_cf_pref_2018[2,1]+am_cf_pref_2018[2,2]+am_cf_pref_2018[2,3]+am_cf_pref_2018[2,4] ///
	+am_cf_pref_2018[3,1]+am_cf_pref_2018[3,2]+am_cf_pref_2018[3,3]+am_cf_pref_2018[3,4] ///
	+am_cf_pref_2018[4,1]+am_cf_pref_2018[4,2]+am_cf_pref_2018[4,3]+am_cf_pref_2018[4,4] ///
	+am_single_m_cf_pref_2018[1,1]+am_single_m_cf_pref_2018[2,1]+am_single_m_cf_pref_2018[3,1]+am_single_m_cf_pref_2018[4,1] ///
	+am_single_f_cf_pref_2018[1,1]+am_single_f_cf_pref_2018[1,2]+am_single_f_cf_pref_2018[1,3]+am_single_f_cf_pref_2018[1,4]
	
sca am_num_2=number_factual/(number_factual+number_counterfactual)

*denominator 2 /*The prob. of being in counterfactual sample*/
sca am_de_2=number_counterfactual/(number_factual+number_counterfactual)


*weights
mat am_w_couples=J(4,4,.)

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat am_w_couples[`i',`j']=(am_num_1_couples[`i',`j']/am_de_1_couples[`i',`j'])*(am_num_2/am_de_2)
	}
}

mat am_w_single_m=J(4,1,.)

forvalues i=1(1)4{
		mat am_w_single_m[`i',1]=(am_num_1_single_m[`i',1]/am_de_1_single_m[`i',1])*(am_num_2/am_de_2)
}

mat am_w_single_f=J(1,4,.)

forvalues i=1(1)4{
		mat am_w_single_f[1,`i']=(am_num_1_single_f[1,`i']/am_de_1_single_f[1,`i'])*(am_num_2/am_de_2)
}

gen weights=.
forvalues i=1(1)4{
	forvalues j=1(1)4{
replace weights=am_w_couples[`i',`j'] if am_male==`i' & am_female==`j' & relationship==1
	}
}

forvalues i=1(1)4{
	replace weights=am_w_single_m[`i',1] if relationship==0 & koen=="1" & ambition_type_k_4_s==`i'
}

forvalues i=1(1)4{
	replace weights=am_w_single_f[1,`i'] if relationship==0 & koen=="2" & ambition_type_k_4_s==`i'
}

replace weights=1 if weights==. /*Then those with missing type are counted same way in factual and counterfactual scenario*/

*Inequality 
forvalues y=2018(1)2018{
qui ineqdeco household_earnings [weight=weights] if aar==`y'

sca gini_cf_pref_`y'=r(gini)
sca p90p50_cf_pref_`y'=r(p90p50)
sca p50p10_cf_pref_`y'=r(p50)/r(p10)
}


frame change results

gen gini_cf_pref=.
forvalues i=2018(1)2018{
replace gini_cf_pref=gini_cf_pref_`i' if aar==`i' 
}

gen p90p50_cf_pref=.
forvalues i=2018(1)2018{
replace p90p50_cf_pref=p90p50_cf_pref_`i' if aar==`i' 
}

gen p50p10_cf_pref=.
forvalues i=2018(1)2018{
replace p50p10_cf_pref=p50p10_cf_pref_`i' if aar==`i' 
}

frame change default


drop weights


*obtain weights - education

*numerator 1
mat educ_num_1_couples=J(4,4,.) /*The prob. of counterfactual given couple cell x,x*/

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat educ_num_1_couples[`i',`j']=educ_cf_pref_2018[`i',`j']/(educ_cf_pref_2018[`i',`j']+educ_2018[`i',`j'])
	}
}

mat educ_num_1_single_m=J(4,1,.) /*The prob. of counterfactual given single men cell x*/

forvalues i=1(1)4{
	mat educ_num_1_single_m[`i',1]=educ_single_m_cf_pref_2018[`i',1]/(educ_single_m_cf_pref_2018[`i',1]+educ_single_m_2018[`i',1])
}

mat educ_num_1_single_f=J(1,4,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)4{
	mat educ_num_1_single_f[1,`i']=educ_single_f_cf_pref_2018[1,`i']/(educ_single_f_cf_pref_2018[1,`i']+educ_single_f_2018[1,`i'])
}

*denominator 1
mat educ_de_1_couples=J(4,4,.) /*The prob. of factual given couple cell x,x*/

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat educ_de_1_couples[`i',`j']=educ_2018[`i',`j']/(educ_cf_pref_2018[`i',`j']+educ_2018[`i',`j'])
	}
}

mat educ_de_1_single_m=J(4,1,.) /*The prob. of factual given single men cell x*/

forvalues i=1(1)4{
	mat educ_de_1_single_m[`i',1]=educ_single_m_2018[`i',1]/(educ_single_m_cf_pref_2018[`i',1]+educ_single_m_2018[`i',1])
}

mat educ_de_1_single_f=J(1,4,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)4{
	mat educ_de_1_single_f[1,`i']=educ_single_f_2018[1,`i']/(educ_single_f_cf_pref_2018[1,`i']+educ_single_f_2018[1,`i'])
}

*numerator 2 /*The prob. of being in factual sample*/

sca number_factual=educ_2018[1,1]+educ_2018[1,2]+educ_2018[1,3]+educ_2018[1,4] ///
	+educ_2018[2,1]+educ_2018[2,2]+educ_2018[2,3]+educ_2018[2,4] ///
	+educ_2018[3,1]+educ_2018[3,2]+educ_2018[3,3]+educ_2018[3,4] ///
	+educ_2018[4,1]+educ_2018[4,2]+educ_2018[4,3]+educ_2018[4,4] ///
	+educ_single_m_2018[1,1]+educ_single_m_2018[2,1]+educ_single_m_2018[3,1]+educ_single_m_2018[4,1] ///
	+educ_single_f_2018[1,1]+educ_single_f_2018[1,2]+educ_single_f_2018[1,3]+educ_single_f_2018[1,4]
	
sca number_counterfactual=educ_cf_pref_2018[1,1]+educ_cf_pref_2018[1,2]+educ_cf_pref_2018[1,3]+educ_cf_pref_2018[1,4] ///
	+educ_cf_pref_2018[2,1]+educ_cf_pref_2018[2,2]+educ_cf_pref_2018[2,3]+educ_cf_pref_2018[2,4] ///
	+educ_cf_pref_2018[3,1]+educ_cf_pref_2018[3,2]+educ_cf_pref_2018[3,3]+educ_cf_pref_2018[3,4] ///
	+educ_cf_pref_2018[4,1]+educ_cf_pref_2018[4,2]+educ_cf_pref_2018[4,3]+educ_cf_pref_2018[4,4] ///
	+educ_single_m_cf_pref_2018[1,1]+educ_single_m_cf_pref_2018[2,1]+educ_single_m_cf_pref_2018[3,1]+educ_single_m_cf_pref_2018[4,1] ///
	+educ_single_f_cf_pref_2018[1,1]+educ_single_f_cf_pref_2018[1,2]+educ_single_f_cf_pref_2018[1,3]+educ_single_f_cf_pref_2018[1,4]
	
sca educ_num_2=number_factual/(number_factual+number_counterfactual)

*denominator 2 /*The prob. of being in counterfactual sample*/
sca educ_de_2=number_counterfactual/(number_factual+number_counterfactual)


*weights
mat educ_w_couples=J(4,4,.)

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat educ_w_couples[`i',`j']=(educ_num_1_couples[`i',`j']/educ_de_1_couples[`i',`j'])*(educ_num_2/educ_de_2)
	}
}

mat educ_w_single_m=J(4,1,.)

forvalues i=1(1)4{
		mat educ_w_single_m[`i',1]=(educ_num_1_single_m[`i',1]/educ_de_1_single_m[`i',1])*(educ_num_2/educ_de_2)
}

mat educ_w_single_f=J(1,4,.)

forvalues i=1(1)4{
		mat educ_w_single_f[1,`i']=(educ_num_1_single_f[1,`i']/educ_de_1_single_f[1,`i'])*(educ_num_2/educ_de_2)
}

gen weights=.
forvalues i=1(1)4{
	forvalues j=1(1)4{
replace weights=educ_w_couples[`i',`j'] if educ_male==`i' & educ_female==`j' & relationship==1
	}
}

forvalues i=1(1)4{
	replace weights=educ_w_single_m[`i',1] if relationship==0 & koen=="1" & fined==`i'
}

forvalues i=1(1)4{
	replace weights=educ_w_single_f[1,`i'] if relationship==0 & koen=="2" & fined==`i'
}

replace weights=1 if weights==. /*Then those with missing type are counted same way in factual and counterfactual scenario*/

*Inequality 
forvalues y=2018(1)2018{
qui ineqdeco household_earnings [weight=weights] if aar==`y'

sca gini_cf_pref_educ_`y'=r(gini)
sca p90p50_cf_pref_educ_`y'=r(p90p50)
sca p50p10_cf_pref_educ_`y'=r(p50)/r(p10)
}


frame change results

gen gini_cf_pref_educ=.
forvalues i=2018(1)2018{
replace gini_cf_pref_educ=gini_cf_pref_educ_`i' if aar==`i' 
}

gen p90p50_cf_pref_educ=.
forvalues i=2018(1)2018{
replace p90p50_cf_pref_educ=p90p50_cf_pref_educ_`i' if aar==`i' 
}

gen p50p10_cf_pref_educ=.
forvalues i=2018(1)2018{
replace p50p10_cf_pref_educ=p50p10_cf_pref_educ_`i' if aar==`i' 
}

frame change default



drop weights


*obtain weights - field

*numerator 1
mat field_num_1_couples=J(8,8,.) /*The prob. of counterfactual given couple cell x,x*/

forvalues i=1(1)8{
	forvalues j=1(1)8{
		mat field_num_1_couples[`i',`j']=field_cf_pref_2018[`i',`j']/(field_cf_pref_2018[`i',`j']+field_2018[`i',`j'])
	}
}

mat field_num_1_single_m=J(8,1,.) /*The prob. of counterfactual given single men cell x*/

forvalues i=1(1)8{
	mat field_num_1_single_m[`i',1]=field_single_m_cf_pref_2018[`i',1]/(field_single_m_cf_pref_2018[`i',1]+field_single_m_2018[`i',1])
}

mat field_num_1_single_f=J(1,8,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)8{
	mat field_num_1_single_f[1,`i']=field_single_f_cf_pref_2018[1,`i']/(field_single_f_cf_pref_2018[1,`i']+field_single_f_2018[1,`i'])
}

*denominator 1
mat field_de_1_couples=J(8,8,.) /*The prob. of factual given couple cell x,x*/

forvalues i=1(1)8{
	forvalues j=1(1)8{
		mat field_de_1_couples[`i',`j']=field_2018[`i',`j']/(field_cf_pref_2018[`i',`j']+field_2018[`i',`j'])
	}
}

mat field_de_1_single_m=J(8,1,.) /*The prob. of factual given single men cell x*/

forvalues i=1(1)8{
	mat field_de_1_single_m[`i',1]=field_single_m_2018[`i',1]/(field_single_m_cf_pref_2018[`i',1]+field_single_m_2018[`i',1])
}

mat field_de_1_single_f=J(1,8,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)8{
	mat field_de_1_single_f[1,`i']=field_single_f_2018[1,`i']/(field_single_f_cf_pref_2018[1,`i']+field_single_f_2018[1,`i'])
}

*numerator 2 /*The prob. of being in factual sample*/

sca number_factual=field_2018[1,1]+field_2018[1,2]+field_2018[1,3]+field_2018[1,4]+field_2018[1,5]+field_2018[1,6]+field_2018[1,7]+field_2018[1,8] ///
	+field_2018[2,1]+field_2018[2,2]+field_2018[2,3]+field_2018[2,4]+field_2018[2,5]+field_2018[2,6]+field_2018[2,7]+field_2018[2,8] ///
	+field_2018[3,1]+field_2018[3,2]+field_2018[3,3]+field_2018[3,4]+field_2018[3,5]+field_2018[3,6]+field_2018[3,7]+field_2018[3,8] ///
	+field_2018[4,1]+field_2018[4,2]+field_2018[4,3]+field_2018[4,4]+field_2018[4,5]+field_2018[4,6]+field_2018[4,7]+field_2018[4,8] ///
	+field_2018[5,1]+field_2018[5,2]+field_2018[5,3]+field_2018[5,4]+field_2018[5,5]+field_2018[5,6]+field_2018[5,7]+field_2018[5,8] ///
	+field_2018[6,1]+field_2018[6,2]+field_2018[6,3]+field_2018[6,4]+field_2018[6,5]+field_2018[6,6]+field_2018[6,7]+field_2018[6,8] ///
	+field_2018[7,1]+field_2018[7,2]+field_2018[7,3]+field_2018[7,4]+field_2018[7,5]+field_2018[7,6]+field_2018[7,7]+field_2018[7,8] ///
	+field_2018[8,1]+field_2018[8,2]+field_2018[8,3]+field_2018[8,4]+field_2018[8,5]+field_2018[8,6]+field_2018[8,7]+field_2018[8,8] ///
	+field_single_m_2018[1,1]+field_single_m_2018[2,1]+field_single_m_2018[3,1]+field_single_m_2018[4,1]+field_single_m_2018[5,1]+field_single_m_2018[6,1]+field_single_m_2018[7,1]+field_single_m_2018[8,1] ///
	+field_single_f_2018[1,1]+field_single_f_2018[1,2]+field_single_f_2018[1,3]+field_single_f_2018[1,4]+field_single_f_2018[1,5]+field_single_f_2018[1,6]+field_single_f_2018[1,7]+field_single_f_2018[1,8]
	
sca number_factual=field_cf_pref_2018[1,1]+field_cf_pref_2018[1,2]+field_cf_pref_2018[1,3]+field_cf_pref_2018[1,4]+field_cf_pref_2018[1,5]+field_cf_pref_2018[1,6]+field_cf_pref_2018[1,7]+field_cf_pref_2018[1,8] ///
	+field_cf_pref_2018[2,1]+field_cf_pref_2018[2,2]+field_cf_pref_2018[2,3]+field_cf_pref_2018[2,4]+field_cf_pref_2018[2,5]+field_cf_pref_2018[2,6]+field_cf_pref_2018[2,7]+field_cf_pref_2018[2,8] ///
	+field_cf_pref_2018[3,1]+field_cf_pref_2018[3,2]+field_cf_pref_2018[3,3]+field_cf_pref_2018[3,4]+field_cf_pref_2018[3,5]+field_cf_pref_2018[3,6]+field_cf_pref_2018[3,7]+field_cf_pref_2018[3,8] ///
	+field_cf_pref_2018[4,1]+field_cf_pref_2018[4,2]+field_cf_pref_2018[4,3]+field_cf_pref_2018[4,4]+field_cf_pref_2018[4,5]+field_cf_pref_2018[4,6]+field_cf_pref_2018[4,7]+field_cf_pref_2018[4,8] ///
	+field_cf_pref_2018[5,1]+field_cf_pref_2018[5,2]+field_cf_pref_2018[5,3]+field_cf_pref_2018[5,4]+field_cf_pref_2018[5,5]+field_cf_pref_2018[5,6]+field_cf_pref_2018[5,7]+field_cf_pref_2018[5,8] ///
	+field_cf_pref_2018[6,1]+field_cf_pref_2018[6,2]+field_cf_pref_2018[6,3]+field_cf_pref_2018[6,4]+field_cf_pref_2018[6,5]+field_cf_pref_2018[6,6]+field_cf_pref_2018[6,7]+field_cf_pref_2018[6,8] ///
	+field_cf_pref_2018[7,1]+field_cf_pref_2018[7,2]+field_cf_pref_2018[7,3]+field_cf_pref_2018[7,4]+field_cf_pref_2018[7,5]+field_cf_pref_2018[7,6]+field_cf_pref_2018[7,7]+field_cf_pref_2018[7,8] ///
	+field_cf_pref_2018[8,1]+field_cf_pref_2018[8,2]+field_cf_pref_2018[8,3]+field_cf_pref_2018[8,4]+field_cf_pref_2018[8,5]+field_cf_pref_2018[8,6]+field_cf_pref_2018[8,7]+field_cf_pref_2018[8,8] ///
	+field_single_m_cf_pref_2018[1,1]+field_single_m_cf_pref_2018[2,1]+field_single_m_cf_pref_2018[3,1]+field_single_m_cf_pref_2018[4,1]+field_single_m_cf_pref_2018[5,1]+field_single_m_cf_pref_2018[6,1]+field_single_m_cf_pref_2018[7,1]+field_single_m_cf_pref_2018[8,1] ///
	+field_single_f_cf_pref_2018[1,1]+field_single_f_cf_pref_2018[1,2]+field_single_f_cf_pref_2018[1,3]+field_single_f_cf_pref_2018[1,4]+field_single_f_cf_pref_2018[1,5]+field_single_f_cf_pref_2018[1,6]+field_single_f_cf_pref_2018[1,7]+field_single_f_cf_pref_2018[1,8]

	
sca field_num_2=number_factual/(number_factual+number_counterfactual)

*denominator 2 /*The prob. of being in counterfactual sample*/
sca field_de_2=number_counterfactual/(number_factual+number_counterfactual)


*weights
mat field_w_couples=J(8,8,.)

forvalues i=1(1)8{
	forvalues j=1(1)8{
		mat field_w_couples[`i',`j']=(field_num_1_couples[`i',`j']/field_de_1_couples[`i',`j'])*(field_num_2/field_de_2)
	}
}

mat field_w_single_m=J(8,1,.)

forvalues i=1(1)8{
		mat field_w_single_m[`i',1]=(field_num_1_single_m[`i',1]/field_de_1_single_m[`i',1])*(field_num_2/field_de_2)
}

mat field_w_single_f=J(1,8,.)

forvalues i=1(1)8{
		mat field_w_single_f[1,`i']=(field_num_1_single_f[1,`i']/field_de_1_single_f[1,`i'])*(field_num_2/field_de_2)
}

gen weights=.
forvalues i=1(1)8{
	forvalues j=1(1)8{
replace weights=field_w_couples[`i',`j'] if field_male==`i' & field_female==`j' & relationship==1
	}
}

forvalues i=1(1)8{
	replace weights=field_w_single_m[`i',1] if relationship==0 & koen=="1" & educ_level_field_num==`i'
}

forvalues i=1(1)8{
	replace weights=field_w_single_f[1,`i'] if relationship==0 & koen=="2" & educ_level_field_num==`i'
}

replace weights=1 if weights==. /*Then those with missing type are counted same way in factual and counterfactual scenario*/

*Inequality 
forvalues y=2018(1)2018{
qui ineqdeco household_earnings [weight=weights] if aar==`y'

sca gini_cf_pref_field_`y'=r(gini)
sca p90p50_cf_pref_field_`y'=r(p90p50)
sca p50p10_cf_pref_field_`y'=r(p50)/r(p10)
}


frame change results

gen gini_cf_pref_field=.
forvalues i=2018(1)2018{
replace gini_cf_pref_field=gini_cf_pref_field_`i' if aar==`i' 
}

gen p90p50_cf_pref_field=.
forvalues i=2018(1)2018{
replace p90p50_cf_pref_field=p90p50_cf_pref_field_`i' if aar==`i' 
}

gen p50p10_cf_pref_field=.
forvalues i=2018(1)2018{
replace p50p10_cf_pref_field=p50p10_cf_pref_field_`i' if aar==`i' 
}

frame change default



drop weights

}


*Fixed marginals*
{
*Solve for 2018 single numbers with 2018 preferences and 1980 marginals (both)*

*ambition types

program nlfaq

syntax varlist(min=1 max=1) [if], at(name)

tempname m1 m2 m3 m4 f1 f2 f3 f4

scalar `m1' = `at'[1,1]
scalar `m2' = `at'[1,2]
scalar `m3' = `at'[1,3]
scalar `m4' = `at'[1,4]
scalar `f1' = `at'[1,5]
scalar `f2' = `at'[1,6]
scalar `f3' = `at'[1,7]
scalar `f4' = `at'[1,8]

tempvar yh
gen double `yh'= am_pi_2018[1,1]*((`m1'*`f1')^(1/2))*((am_mar_m_base[1,1]*am_mar_f_base[1,1])^((1/2)))+am_pi_2018[1,2]*((`m1'*`f2')^(1/2))*(((am_mar_m_base[1,1]*am_mar_f_base[1,2]))^((1/2)))+am_pi_2018[1,3]*((`m1'*`f3')^(1/2))*(((am_mar_m_base[1,1]*am_mar_f_base[1,3]))^((1/2)))+am_pi_2018[1,4]*((`m1'*`f4')^(1/2))*(((am_mar_m_base[1,1]*am_mar_f_base[1,4]))^((1/2)))+`m1'-am_mar_m_base[1,1]+1 in 1

replace `yh'= am_pi_2018[2,1]*((`m2'*`f1')^(1/2))*(((am_mar_m_base[2,1]*am_mar_f_base[1,1]))^((1/2)))+am_pi_2018[2,2]*((`m2'*`f2')^(1/2))*(((am_mar_m_base[2,1]*am_mar_f_base[1,2]))^((1/2)))+am_pi_2018[2,3]*((`m2'*`f3')^(1/2))*(((am_mar_m_base[2,1]*am_mar_f_base[1,3]))^((1/2)))+am_pi_2018[2,4]*((`m2'*`f4')^(1/2))*(((am_mar_m_base[2,1]*am_mar_f_base[1,4]))^((1/2)))+`m2'-am_mar_m_base[2,1]+1 in 2

replace `yh'= am_pi_2018[3,1]*((`m3'*`f1')^(1/2))*(((am_mar_m_base[3,1]*am_mar_f_base[1,1]))^((1/2)))+am_pi_2018[3,2]*((`m3'*`f2')^(1/2))*(((am_mar_m_base[3,1]*am_mar_f_base[1,2]))^((1/2)))+am_pi_2018[3,3]*((`m3'*`f3')^(1/2))*(((am_mar_m_base[3,1]*am_mar_f_base[1,3]))^((1/2)))+am_pi_2018[3,4]*((`m3'*`f4')^(1/2))*(((am_mar_m_base[3,1]*am_mar_f_base[1,4]))^((1/2)))+`m3'-am_mar_m_base[3,1]+1 in 3

replace `yh'= am_pi_2018[4,1]*((`m4'*`f1')^(1/2))*(((am_mar_m_base[4,1]*am_mar_f_base[1,1]))^((1/2)))+am_pi_2018[4,2]*((`m4'*`f2')^(1/2))*(((am_mar_m_base[4,1]*am_mar_f_base[1,2]))^((1/2)))+am_pi_2018[4,3]*((`m4'*`f3')^(1/2))*(((am_mar_m_base[4,1]*am_mar_f_base[1,3]))^((1/2)))+am_pi_2018[4,4]*((`m4'*`f4')^(1/2))*(((am_mar_m_base[4,1]*am_mar_f_base[1,4]))^((1/2)))+`m4'-am_mar_m_base[4,1]+1 in 4

replace `yh'=am_pi_2018[1,1]*((`m1'*`f1')^(1/2))*(((am_mar_m_base[1,1]*am_mar_f_base[1,1]))^((1/2)))+am_pi_2018[2,1]*((`m2'*`f1')^(1/2))*(((am_mar_m_base[2,1]*am_mar_f_base[1,1]))^((1/2)))+am_pi_2018[3,1]*((`m3'*`f1')^(1/2))*(((am_mar_m_base[3,1]*am_mar_f_base[1,1]))^((1/2)))+am_pi_2018[4,1]*((`m4'*`f1')^(1/2))*(((am_mar_m_base[4,1]*am_mar_f_base[1,1]))^((1/2)))+`f1'-am_mar_f_base[1,1] in 5

replace `yh'=am_pi_2018[1,2]*((`m1'*`f2')^(1/2))*(((am_mar_m_base[1,1]*am_mar_f_base[1,2]))^((1/2)))+am_pi_2018[2,2]*((`m2'*`f2')^(1/2))*(((am_mar_m_base[2,1]*am_mar_f_base[1,2]))^((1/2)))+am_pi_2018[3,2]*((`m3'*`f2')^(1/2))*(((am_mar_m_base[3,1]*am_mar_f_base[1,2]))^((1/2)))+am_pi_2018[4,2]*((`m4'*`f2')^(1/2))*(((am_mar_m_base[4,1]*am_mar_f_base[1,2]))^((1/2)))+`f2'-am_mar_f_base[1,2] in 6

replace `yh'=am_pi_2018[1,3]*((`m1'*`f3')^(1/2))*(((am_mar_m_base[1,1]*am_mar_f_base[1,3]))^((1/2)))+am_pi_2018[2,3]*((`m2'*`f3')^(1/2))*(((am_mar_m_base[2,1]*am_mar_f_base[1,3]))^((1/2)))+am_pi_2018[3,3]*((`m3'*`f3')^(1/2))*(((am_mar_m_base[3,1]*am_mar_f_base[1,3]))^((1/2)))+am_pi_2018[4,3]*((`m4'*`f3')^(1/2))*(((am_mar_m_base[4,1]*am_mar_f_base[1,3]))^((1/2)))+`f3'-am_mar_f_base[1,3] in 7

replace `yh'=am_pi_2018[1,4]*((`m1'*`f4')^(1/2))*(((am_mar_m_base[1,1]*am_mar_f_base[1,4]))^((1/2)))+am_pi_2018[2,4]*((`m2'*`f4')^(1/2))*(((am_mar_m_base[2,1]*am_mar_f_base[1,4]))^((1/2)))+am_pi_2018[3,4]*((`m3'*`f4')^(1/2))*(((am_mar_m_base[3,1]*am_mar_f_base[1,4]))^((1/2)))+am_pi_2018[4,4]*((`m4'*`f4')^(1/2))*(((am_mar_m_base[4,1]*am_mar_f_base[1,4]))^((1/2)))+`f4'-am_mar_f_base[1,4] in 8


replace `varlist'=`yh'
end


frame create temp
frame change temp

clear
set obs 8

gen y = 0
replace y=1 in 1

nl faq @ y, parameters(m1 m2 m3 m4 f1 f2 f3 f4) initial(m1 1 m2 1 m3 1 m4 1 f1 1 f2 1 f3 1 f4 1)

*get counter factual singles
mat am_single_m_cf_mar_2018=J(4,1,.)
mat am_single_m_cf_mar_2018[1,1]=[m1]_b[_cons]
mat am_single_m_cf_mar_2018[2,1]=[m2]_b[_cons]
mat am_single_m_cf_mar_2018[3,1]=[m3]_b[_cons]
mat am_single_m_cf_mar_2018[4,1]=[m4]_b[_cons]

mat am_single_f_cf_mar_2018=J(1,4,.)
mat am_single_f_cf_mar_2018[1,1]=[f1]_b[_cons]
mat am_single_f_cf_mar_2018[1,2]=[f2]_b[_cons]
mat am_single_f_cf_mar_2018[1,3]=[f3]_b[_cons]
mat am_single_f_cf_mar_2018[1,4]=[f4]_b[_cons]

frame change default
frame drop temp
prog drop nlfaq




*education types
program nlfaq

syntax varlist(min=1 max=1) [if], at(name)

tempname m1 m2 m3 m4 f1 f2 f3 f4

scalar `m1' = `at'[1,1]
scalar `m2' = `at'[1,2]
scalar `m3' = `at'[1,3]
scalar `m4' = `at'[1,4]
scalar `f1' = `at'[1,5]
scalar `f2' = `at'[1,6]
scalar `f3' = `at'[1,7]
scalar `f4' = `at'[1,8]

tempvar yh
gen double `yh'= educ_pi_2018[1,1]*((`m1'*`f1')^(1/2))*((educ_mar_m_base[1,1]*educ_mar_f_base[1,1])^((1/2)))+educ_pi_2018[1,2]*((`m1'*`f2')^(1/2))*(((educ_mar_m_base[1,1]*educ_mar_f_base[1,2]))^((1/2)))+educ_pi_2018[1,3]*((`m1'*`f3')^(1/2))*(((educ_mar_m_base[1,1]*educ_mar_f_base[1,3]))^((1/2)))+educ_pi_2018[1,4]*((`m1'*`f4')^(1/2))*(((educ_mar_m_base[1,1]*educ_mar_f_base[1,4]))^((1/2)))+`m1'-educ_mar_m_base[1,1]+1 in 1

replace `yh'= educ_pi_2018[2,1]*((`m2'*`f1')^(1/2))*(((educ_mar_m_base[2,1]*educ_mar_f_base[1,1]))^((1/2)))+educ_pi_2018[2,2]*((`m2'*`f2')^(1/2))*(((educ_mar_m_base[2,1]*educ_mar_f_base[1,2]))^((1/2)))+educ_pi_2018[2,3]*((`m2'*`f3')^(1/2))*(((educ_mar_m_base[2,1]*educ_mar_f_base[1,3]))^((1/2)))+educ_pi_2018[2,4]*((`m2'*`f4')^(1/2))*(((educ_mar_m_base[2,1]*educ_mar_f_base[1,4]))^((1/2)))+`m2'-educ_mar_m_base[2,1]+1 in 2

replace `yh'= educ_pi_2018[3,1]*((`m3'*`f1')^(1/2))*(((educ_mar_m_base[3,1]*educ_mar_f_base[1,1]))^((1/2)))+educ_pi_2018[3,2]*((`m3'*`f2')^(1/2))*(((educ_mar_m_base[3,1]*educ_mar_f_base[1,2]))^((1/2)))+educ_pi_2018[3,3]*((`m3'*`f3')^(1/2))*(((educ_mar_m_base[3,1]*educ_mar_f_base[1,3]))^((1/2)))+educ_pi_2018[3,4]*((`m3'*`f4')^(1/2))*(((educ_mar_m_base[3,1]*educ_mar_f_base[1,4]))^((1/2)))+`m3'-educ_mar_m_base[3,1]+1 in 3

replace `yh'= educ_pi_2018[4,1]*((`m4'*`f1')^(1/2))*(((educ_mar_m_base[4,1]*educ_mar_f_base[1,1]))^((1/2)))+educ_pi_2018[4,2]*((`m4'*`f2')^(1/2))*(((educ_mar_m_base[4,1]*educ_mar_f_base[1,2]))^((1/2)))+educ_pi_2018[4,3]*((`m4'*`f3')^(1/2))*(((educ_mar_m_base[4,1]*educ_mar_f_base[1,3]))^((1/2)))+educ_pi_2018[4,4]*((`m4'*`f4')^(1/2))*(((educ_mar_m_base[4,1]*educ_mar_f_base[1,4]))^((1/2)))+`m4'-educ_mar_m_base[4,1]+1 in 4

replace `yh'=educ_pi_2018[1,1]*((`m1'*`f1')^(1/2))*(((educ_mar_m_base[1,1]*educ_mar_f_base[1,1]))^((1/2)))+educ_pi_2018[2,1]*((`m2'*`f1')^(1/2))*(((educ_mar_m_base[2,1]*educ_mar_f_base[1,1]))^((1/2)))+educ_pi_2018[3,1]*((`m3'*`f1')^(1/2))*(((educ_mar_m_base[3,1]*educ_mar_f_base[1,1]))^((1/2)))+educ_pi_2018[4,1]*((`m4'*`f1')^(1/2))*(((educ_mar_m_base[4,1]*educ_mar_f_base[1,1]))^((1/2)))+`f1'-educ_mar_f_base[1,1] in 5

replace `yh'=educ_pi_2018[1,2]*((`m1'*`f2')^(1/2))*(((educ_mar_m_base[1,1]*educ_mar_f_base[1,2]))^((1/2)))+educ_pi_2018[2,2]*((`m2'*`f2')^(1/2))*(((educ_mar_m_base[2,1]*educ_mar_f_base[1,2]))^((1/2)))+educ_pi_2018[3,2]*((`m3'*`f2')^(1/2))*(((educ_mar_m_base[3,1]*educ_mar_f_base[1,2]))^((1/2)))+educ_pi_2018[4,2]*((`m4'*`f2')^(1/2))*(((educ_mar_m_base[4,1]*educ_mar_f_base[1,2]))^((1/2)))+`f2'-educ_mar_f_base[1,2] in 6

replace `yh'=educ_pi_2018[1,3]*((`m1'*`f3')^(1/2))*(((educ_mar_m_base[1,1]*educ_mar_f_base[1,3]))^((1/2)))+educ_pi_2018[2,3]*((`m2'*`f3')^(1/2))*(((educ_mar_m_base[2,1]*educ_mar_f_base[1,3]))^((1/2)))+educ_pi_2018[3,3]*((`m3'*`f3')^(1/2))*(((educ_mar_m_base[3,1]*educ_mar_f_base[1,3]))^((1/2)))+educ_pi_2018[4,3]*((`m4'*`f3')^(1/2))*(((educ_mar_m_base[4,1]*educ_mar_f_base[1,3]))^((1/2)))+`f3'-educ_mar_f_base[1,3] in 7

replace `yh'=educ_pi_2018[1,4]*((`m1'*`f4')^(1/2))*(((educ_mar_m_base[1,1]*educ_mar_f_base[1,4]))^((1/2)))+educ_pi_2018[2,4]*((`m2'*`f4')^(1/2))*(((educ_mar_m_base[2,1]*educ_mar_f_base[1,4]))^((1/2)))+educ_pi_2018[3,4]*((`m3'*`f4')^(1/2))*(((educ_mar_m_base[3,1]*educ_mar_f_base[1,4]))^((1/2)))+educ_pi_2018[4,4]*((`m4'*`f4')^(1/2))*(((educ_mar_m_base[4,1]*educ_mar_f_base[1,4]))^((1/2)))+`f4'-educ_mar_f_base[1,4] in 8

replace `varlist'=`yh'
end


frame create temp
frame change temp

clear
set obs 8

gen y = 0
replace y=1 in 1

nl faq @ y, parameters(m1 m2 m3 m4 f1 f2 f3 f4) initial(m1 1 m2 1 m3 1 m4 1 f1 1 f2 1 f3 1 f4 1)

*get counter factual singles
mat educ_single_m_cf_mar_2018=J(4,1,.)
mat educ_single_m_cf_mar_2018[1,1]=[m1]_b[_cons]
mat educ_single_m_cf_mar_2018[2,1]=[m2]_b[_cons]
mat educ_single_m_cf_mar_2018[3,1]=[m3]_b[_cons]
mat educ_single_m_cf_mar_2018[4,1]=[m4]_b[_cons]

mat educ_single_f_cf_mar_2018=J(1,4,.)
mat educ_single_f_cf_mar_2018[1,1]=[f1]_b[_cons]
mat educ_single_f_cf_mar_2018[1,2]=[f2]_b[_cons]
mat educ_single_f_cf_mar_2018[1,3]=[f3]_b[_cons]
mat educ_single_f_cf_mar_2018[1,4]=[f4]_b[_cons]


frame change default
frame drop temp
prog drop nlfaq

*field types
program nlfaq

syntax varlist(min=1 max=1) [if], at(name)

tempname m1 m2 m3 m4 m5 m6 m7 m8 f1 f2 f3 f4 f5 f6 f7 f8

scalar `m1' = `at'[1,1]
scalar `m2' = `at'[1,2]
scalar `m3' = `at'[1,3]
scalar `m4' = `at'[1,4]
scalar `m5' = `at'[1,5]
scalar `m6' = `at'[1,6]
scalar `m7' = `at'[1,7]
scalar `m8' = `at'[1,8]
scalar `f1' = `at'[1,9]
scalar `f2' = `at'[1,10]
scalar `f3' = `at'[1,11]
scalar `f4' = `at'[1,12]
scalar `f5' = `at'[1,13]
scalar `f6' = `at'[1,14]
scalar `f7' = `at'[1,15]
scalar `f8' = `at'[1,16]



tempvar yh
gen double `yh'= field_pi_2018[1,1]*((`m1'*`f1')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[1,2]*((`m1'*`f2')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[1,3]*((`m1'*`f3')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[1,4]*((`m1'*`f4')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[1,5]*((`m1'*`f5')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[1,6]*((`m1'*`f6')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[1,7]*((`m1'*`f7')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[1,8]*((`m1'*`f8')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_base[1,8]))^((1/2)))+`m1'-field_mar_m_base[1,1]+1 in 1

replace `yh'= field_pi_2018[2,1]*((`m2'*`f1')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[2,2]*((`m2'*`f2')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[2,3]*((`m2'*`f3')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[2,4]*((`m2'*`f4')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[2,5]*((`m2'*`f5')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[2,6]*((`m2'*`f6')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[2,7]*((`m2'*`f7')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[2,8]*((`m2'*`f8')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_base[1,8]))^((1/2)))+`m2'-field_mar_m_base[2,1]+1 in 2

replace `yh'= field_pi_2018[3,1]*((`m3'*`f1')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[3,2]*((`m3'*`f2')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[3,3]*((`m3'*`f3')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[3,4]*((`m3'*`f4')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[3,5]*((`m3'*`f5')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[3,6]*((`m3'*`f6')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[3,7]*((`m3'*`f7')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[3,8]*((`m3'*`f8')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_base[1,8]))^((1/2)))+`m3'-field_mar_m_base[3,1]+1 in 3

replace `yh'= field_pi_2018[4,1]*((`m4'*`f1')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[4,2]*((`m4'*`f2')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[4,3]*((`m4'*`f3')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[4,4]*((`m4'*`f4')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[4,5]*((`m4'*`f5')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[4,6]*((`m4'*`f6')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[4,7]*((`m4'*`f7')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[4,8]*((`m4'*`f8')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_base[1,8]))^((1/2)))+`m4'-field_mar_m_base[4,1]+1 in 4

replace `yh'= field_pi_2018[5,1]*((`m5'*`f1')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[5,2]*((`m5'*`f2')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[5,3]*((`m5'*`f3')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[5,4]*((`m5'*`f4')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[5,5]*((`m5'*`f5')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[5,6]*((`m5'*`f6')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[5,7]*((`m5'*`f7')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[5,8]*((`m5'*`f8')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_base[1,8]))^((1/2)))+`m5'-field_mar_m_base[5,1]+1 in 5

replace `yh'= field_pi_2018[6,1]*((`m6'*`f1')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[6,2]*((`m6'*`f2')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[6,3]*((`m6'*`f3')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[6,4]*((`m6'*`f4')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[6,5]*((`m6'*`f5')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[6,6]*((`m6'*`f6')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[6,7]*((`m6'*`f7')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[6,8]*((`m6'*`f8')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_base[1,8]))^((1/2)))+`m6'-field_mar_m_base[6,1]+1 in 6

replace `yh'= field_pi_2018[7,1]*((`m7'*`f1')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[7,2]*((`m7'*`f2')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[7,3]*((`m7'*`f3')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[7,4]*((`m7'*`f4')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[7,5]*((`m7'*`f5')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[7,6]*((`m7'*`f6')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[7,7]*((`m7'*`f7')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[7,8]*((`m7'*`f8')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_base[1,8]))^((1/2)))+`m7'-field_mar_m_base[7,1]+1 in 7

replace `yh'= field_pi_2018[8,1]*((`m8'*`f1')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[8,2]*((`m8'*`f2')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[8,3]*((`m8'*`f3')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[8,4]*((`m8'*`f4')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[8,5]*((`m8'*`f5')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[8,6]*((`m8'*`f6')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[8,7]*((`m8'*`f7')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[8,8]*((`m8'*`f8')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_base[1,8]))^((1/2)))+`m8'-field_mar_m_base[8,1]+1 in 8

replace `yh'=field_pi_2018[1,1]*((`m1'*`f1')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[2,1]*((`m2'*`f1')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[3,1]*((`m3'*`f1')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[4,1]*((`m4'*`f1')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[5,1]*((`m5'*`f1')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[6,1]*((`m6'*`f1')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[7,1]*((`m7'*`f1')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[8,1]*((`m8'*`f1')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_base[1,1]))^((1/2)))+`f1'-field_mar_f_base[1,1] in 9

replace `yh'=field_pi_2018[1,2]*((`m1'*`f2')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[2,2]*((`m2'*`f2')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[3,2]*((`m3'*`f2')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[4,2]*((`m4'*`f2')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[5,2]*((`m5'*`f2')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[6,2]*((`m6'*`f2')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[7,2]*((`m7'*`f2')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[8,2]*((`m8'*`f2')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_base[1,2]))^((1/2)))+`f2'-field_mar_f_base[1,2] in 10

replace `yh'=field_pi_2018[1,3]*((`m1'*`f3')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[2,3]*((`m2'*`f3')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[3,3]*((`m3'*`f3')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[4,3]*((`m4'*`f3')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[5,3]*((`m5'*`f3')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[6,3]*((`m6'*`f3')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[7,3]*((`m7'*`f3')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[8,3]*((`m8'*`f3')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_base[1,3]))^((1/2)))+`f3'-field_mar_f_base[1,3] in 11

replace `yh'=field_pi_2018[1,4]*((`m1'*`f4')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[2,4]*((`m2'*`f4')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[3,4]*((`m3'*`f4')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[4,4]*((`m4'*`f4')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[5,4]*((`m5'*`f4')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[6,4]*((`m6'*`f4')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[7,4]*((`m7'*`f4')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[8,4]*((`m8'*`f4')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_base[1,4]))^((1/2)))+`f4'-field_mar_f_base[1,4] in 12

replace `yh'=field_pi_2018[1,5]*((`m1'*`f5')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[2,5]*((`m2'*`f5')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[3,5]*((`m3'*`f5')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[4,5]*((`m4'*`f5')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[5,5]*((`m5'*`f5')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[6,5]*((`m6'*`f5')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[7,5]*((`m7'*`f5')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[8,5]*((`m8'*`f5')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_base[1,5]))^((1/2)))+`f5'-field_mar_f_base[1,5] in 13

replace `yh'=field_pi_2018[1,6]*((`m1'*`f6')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[2,6]*((`m2'*`f6')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[3,6]*((`m3'*`f6')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[4,6]*((`m4'*`f6')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[5,6]*((`m5'*`f6')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[6,6]*((`m6'*`f6')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[7,6]*((`m7'*`f6')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[8,6]*((`m8'*`f6')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_base[1,6]))^((1/2)))+`f6'-field_mar_f_base[1,6] in 14

replace `yh'=field_pi_2018[1,7]*((`m1'*`f7')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[2,7]*((`m2'*`f7')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[3,7]*((`m3'*`f7')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[4,7]*((`m4'*`f7')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[5,7]*((`m5'*`f7')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[6,7]*((`m6'*`f7')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[7,7]*((`m7'*`f7')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[8,7]*((`m8'*`f7')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_base[1,7]))^((1/2)))+`f7'-field_mar_f_base[1,7] in 15

replace `yh'=field_pi_2018[1,8]*((`m1'*`f8')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_base[1,8]))^((1/2)))+field_pi_2018[2,8]*((`m2'*`f8')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_base[1,8]))^((1/2)))+field_pi_2018[3,8]*((`m3'*`f8')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_base[1,8]))^((1/2)))+field_pi_2018[4,8]*((`m4'*`f8')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_base[1,8]))^((1/2)))+field_pi_2018[5,8]*((`m5'*`f8')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_base[1,8]))^((1/2)))+field_pi_2018[6,8]*((`m6'*`f8')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_base[1,8]))^((1/2)))+field_pi_2018[7,8]*((`m7'*`f8')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_base[1,8]))^((1/2)))+field_pi_2018[8,8]*((`m8'*`f8')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_base[1,8]))^((1/2)))+`f8'-field_mar_f_base[1,8] in 16



replace `varlist'=`yh'
end


frame create temp
frame change temp

clear
set obs 16

gen y = 0
replace y=1 in 1

nl faq @ y, parameters(m1 m2 m3 m4 m5 m6 m7 m8 f1 f2 f3 f4 f5 f6 f7 f8) initial(m1 1 m2 1 m3 1 m4 1 m5 1 m6 1 m7 1 m8 1 f1 1 f2 1 f3 1 f4 1 f5 1 f6 1 f7 1 f8 1)

*get counter factual singles
mat field_single_m_cf_mar_2018=J(8,1,.)
mat field_single_m_cf_mar_2018[1,1]=[m1]_b[_cons]
mat field_single_m_cf_mar_2018[2,1]=[m2]_b[_cons]
mat field_single_m_cf_mar_2018[3,1]=[m3]_b[_cons]
mat field_single_m_cf_mar_2018[4,1]=[m4]_b[_cons]
mat field_single_m_cf_mar_2018[5,1]=[m5]_b[_cons]
mat field_single_m_cf_mar_2018[6,1]=[m6]_b[_cons]
mat field_single_m_cf_mar_2018[7,1]=[m7]_b[_cons]
mat field_single_m_cf_mar_2018[8,1]=[m8]_b[_cons]

mat field_single_f_cf_mar_2018=J(1,8,.)
mat field_single_f_cf_mar_2018[1,1]=[f1]_b[_cons]
mat field_single_f_cf_mar_2018[1,2]=[f2]_b[_cons]
mat field_single_f_cf_mar_2018[1,3]=[f3]_b[_cons]
mat field_single_f_cf_mar_2018[1,4]=[f4]_b[_cons]
mat field_single_f_cf_mar_2018[1,5]=[f5]_b[_cons]
mat field_single_f_cf_mar_2018[1,6]=[f6]_b[_cons]
mat field_single_f_cf_mar_2018[1,7]=[f7]_b[_cons]
mat field_single_f_cf_mar_2018[1,8]=[f8]_b[_cons]


frame change default
frame drop temp
prog drop nlfaq

*get counter factual couple numbers*

mat am_cf_mar_2018=J(4,4,.)

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat am_cf_mar_2018[`i',`j']=am_pi_2018[`i',`j']*((am_single_m_cf_mar_2018[`i',1]*am_single_f_cf_mar_2018[1,`j'])^(1/2))*((am_mar_m_base[`i',1]*am_mar_f_base[1,`j'])^((1/2)))
	}
}

mat educ_cf_mar_2018=J(4,4,.)

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat educ_cf_mar_2018[`i',`j']=educ_pi_2018[`i',`j']*((educ_single_m_cf_mar_2018[`i',1]*educ_single_f_cf_mar_2018[1,`j'])^(1/2))*((educ_mar_m_base[`i',1]*educ_mar_f_base[1,`j'])^((1/2)))
	}
}

mat field_cf_mar_2018=J(8,8,.)

forvalues i=1(1)8{
	forvalues j=1(1)8{
		mat field_cf_mar_2018[`i',`j']=field_pi_2018[`i',`j']*((field_single_m_cf_mar_2018[`i',1]*field_single_f_cf_mar_2018[1,`j'])^(1/2))*((field_mar_m_base[`i',1]*field_mar_f_base[1,`j'])^((1/2)))
	}
}


*obtain weights - ambition

*numerator 1
mat am_num_1_couples=J(4,4,.) /*The prob. of counterfactual given couple cell x,x*/

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat am_num_1_couples[`i',`j']=am_cf_mar_2018[`i',`j']/(am_cf_mar_2018[`i',`j']+am_2018[`i',`j'])
	}
}

mat am_num_1_single_m=J(4,1,.) /*The prob. of counterfactual given single men cell x*/

forvalues i=1(1)4{
	mat am_num_1_single_m[`i',1]=am_single_m_cf_mar_2018[`i',1]/(am_single_m_cf_mar_2018[`i',1]+am_single_m_2018[`i',1])
}

mat am_num_1_single_f=J(1,4,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)4{
	mat am_num_1_single_f[1,`i']=am_single_f_cf_mar_2018[1,`i']/(am_single_f_cf_mar_2018[1,`i']+am_single_f_2018[1,`i'])
}

*denominator 1
mat am_de_1_couples=J(4,4,.) /*The prob. of factual given couple cell x,x*/

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat am_de_1_couples[`i',`j']=am_2018[`i',`j']/(am_cf_mar_2018[`i',`j']+am_2018[`i',`j'])
	}
}

mat am_de_1_single_m=J(4,1,.) /*The prob. of factual given single men cell x*/

forvalues i=1(1)4{
	mat am_de_1_single_m[`i',1]=am_single_m_2018[`i',1]/(am_single_m_cf_mar_2018[`i',1]+am_single_m_2018[`i',1])
}

mat am_de_1_single_f=J(1,4,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)4{
	mat am_de_1_single_f[1,`i']=am_single_f_2018[1,`i']/(am_single_f_cf_mar_2018[1,`i']+am_single_f_2018[1,`i'])
}

*numerator 2 /*The prob. of being in factual sample*/

sca number_factual=am_2018[1,1]+am_2018[1,2]+am_2018[1,3]+am_2018[1,4] ///
	+am_2018[2,1]+am_2018[2,2]+am_2018[2,3]+am_2018[2,4] ///
	+am_2018[3,1]+am_2018[3,2]+am_2018[3,3]+am_2018[3,4] ///
	+am_2018[4,1]+am_2018[4,2]+am_2018[4,3]+am_2018[4,4] ///
	+am_single_m_2018[1,1]+am_single_m_2018[2,1]+am_single_m_2018[3,1]+am_single_m_2018[4,1] ///
	+am_single_f_2018[1,1]+am_single_f_2018[1,2]+am_single_f_2018[1,3]+am_single_f_2018[1,4]
	
sca number_counterfactual=am_cf_mar_2018[1,1]+am_cf_mar_2018[1,2]+am_cf_mar_2018[1,3]+am_cf_mar_2018[1,4] ///
	+am_cf_mar_2018[2,1]+am_cf_mar_2018[2,2]+am_cf_mar_2018[2,3]+am_cf_mar_2018[2,4] ///
	+am_cf_mar_2018[3,1]+am_cf_mar_2018[3,2]+am_cf_mar_2018[3,3]+am_cf_mar_2018[3,4] ///
	+am_cf_mar_2018[4,1]+am_cf_mar_2018[4,2]+am_cf_mar_2018[4,3]+am_cf_mar_2018[4,4] ///
	+am_single_m_cf_mar_2018[1,1]+am_single_m_cf_mar_2018[2,1]+am_single_m_cf_mar_2018[3,1]+am_single_m_cf_mar_2018[4,1] ///
	+am_single_f_cf_mar_2018[1,1]+am_single_f_cf_mar_2018[1,2]+am_single_f_cf_mar_2018[1,3]+am_single_f_cf_mar_2018[1,4]
	
sca am_num_2=number_factual/(number_factual+number_counterfactual)

*denominator 2 /*The prob. of being in counterfactual sample*/
sca am_de_2=number_counterfactual/(number_factual+number_counterfactual)


*weights
mat am_w_couples=J(4,4,.)

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat am_w_couples[`i',`j']=(am_num_1_couples[`i',`j']/am_de_1_couples[`i',`j'])*(am_num_2/am_de_2)
	}
}

mat am_w_single_m=J(4,1,.)

forvalues i=1(1)4{
		mat am_w_single_m[`i',1]=(am_num_1_single_m[`i',1]/am_de_1_single_m[`i',1])*(am_num_2/am_de_2)
}

mat am_w_single_f=J(1,4,.)

forvalues i=1(1)4{
		mat am_w_single_f[1,`i']=(am_num_1_single_f[1,`i']/am_de_1_single_f[1,`i'])*(am_num_2/am_de_2)
}

gen weights=.
forvalues i=1(1)4{
	forvalues j=1(1)4{
replace weights=am_w_couples[`i',`j'] if am_male==`i' & am_female==`j' & relationship==1
	}
}

forvalues i=1(1)4{
	replace weights=am_w_single_m[`i',1] if relationship==0 & koen=="1" & ambition_type_k_4_s==`i'
}

forvalues i=1(1)4{
	replace weights=am_w_single_f[1,`i'] if relationship==0 & koen=="2" & ambition_type_k_4_s==`i'
}

replace weights=1 if weights==. /*Then those with missing type are counted same way in factual and counterfactual scenario*/

*Inequality 
forvalues y=2018(1)2018{
qui ineqdeco household_earnings [weight=weights] if aar==`y'

sca gini_cf_mar_`y'=r(gini)
sca p90p50_cf_mar_`y'=r(p90p50)
sca p50p10_cf_mar_`y'=r(p50)/r(p10)
}


frame change results

gen gini_cf_mar=.
forvalues i=2018(1)2018{
replace gini_cf_mar=gini_cf_mar_`i' if aar==`i' 
}

gen p90p50_cf_mar=.
forvalues i=2018(1)2018{
replace p90p50_cf_mar=p90p50_cf_mar_`i' if aar==`i' 
}

gen p50p10_cf_mar=.
forvalues i=2018(1)2018{
replace p50p10_cf_mar=p50p10_cf_mar_`i' if aar==`i' 
}

frame change default



drop weights


*obtain weights - education

*numerator 1
mat educ_num_1_couples=J(4,4,.) /*The prob. of counterfactual given couple cell x,x*/

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat educ_num_1_couples[`i',`j']=educ_cf_mar_2018[`i',`j']/(educ_cf_mar_2018[`i',`j']+educ_2018[`i',`j'])
	}
}

mat educ_num_1_single_m=J(4,1,.) /*The prob. of counterfactual given single men cell x*/

forvalues i=1(1)4{
	mat educ_num_1_single_m[`i',1]=educ_single_m_cf_mar_2018[`i',1]/(educ_single_m_cf_mar_2018[`i',1]+educ_single_m_2018[`i',1])
}

mat educ_num_1_single_f=J(1,4,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)4{
	mat educ_num_1_single_f[1,`i']=educ_single_f_cf_mar_2018[1,`i']/(educ_single_f_cf_mar_2018[1,`i']+educ_single_f_2018[1,`i'])
}

*denominator 1
mat educ_de_1_couples=J(4,4,.) /*The prob. of factual given couple cell x,x*/

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat educ_de_1_couples[`i',`j']=educ_2018[`i',`j']/(educ_cf_mar_2018[`i',`j']+educ_2018[`i',`j'])
	}
}

mat educ_de_1_single_m=J(4,1,.) /*The prob. of factual given single men cell x*/

forvalues i=1(1)4{
	mat educ_de_1_single_m[`i',1]=educ_single_m_2018[`i',1]/(educ_single_m_cf_mar_2018[`i',1]+educ_single_m_2018[`i',1])
}

mat educ_de_1_single_f=J(1,4,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)4{
	mat educ_de_1_single_f[1,`i']=educ_single_f_2018[1,`i']/(educ_single_f_cf_mar_2018[1,`i']+educ_single_f_2018[1,`i'])
}

*numerator 2 /*The prob. of being in factual sample*/

sca number_factual=educ_2018[1,1]+educ_2018[1,2]+educ_2018[1,3]+educ_2018[1,4] ///
	+educ_2018[2,1]+educ_2018[2,2]+educ_2018[2,3]+educ_2018[2,4] ///
	+educ_2018[3,1]+educ_2018[3,2]+educ_2018[3,3]+educ_2018[3,4] ///
	+educ_2018[4,1]+educ_2018[4,2]+educ_2018[4,3]+educ_2018[4,4] ///
	+educ_single_m_2018[1,1]+educ_single_m_2018[2,1]+educ_single_m_2018[3,1]+educ_single_m_2018[4,1] ///
	+educ_single_f_2018[1,1]+educ_single_f_2018[1,2]+educ_single_f_2018[1,3]+educ_single_f_2018[1,4]
	
sca number_counterfactual=educ_cf_mar_2018[1,1]+educ_cf_mar_2018[1,2]+educ_cf_mar_2018[1,3]+educ_cf_mar_2018[1,4] ///
	+educ_cf_mar_2018[2,1]+educ_cf_mar_2018[2,2]+educ_cf_mar_2018[2,3]+educ_cf_mar_2018[2,4] ///
	+educ_cf_mar_2018[3,1]+educ_cf_mar_2018[3,2]+educ_cf_mar_2018[3,3]+educ_cf_mar_2018[3,4] ///
	+educ_cf_mar_2018[4,1]+educ_cf_mar_2018[4,2]+educ_cf_mar_2018[4,3]+educ_cf_mar_2018[4,4] ///
	+educ_single_m_cf_mar_2018[1,1]+educ_single_m_cf_mar_2018[2,1]+educ_single_m_cf_mar_2018[3,1]+educ_single_m_cf_mar_2018[4,1] ///
	+educ_single_f_cf_mar_2018[1,1]+educ_single_f_cf_mar_2018[1,2]+educ_single_f_cf_mar_2018[1,3]+educ_single_f_cf_mar_2018[1,4]
	
sca educ_num_2=number_factual/(number_factual+number_counterfactual)

*denominator 2 /*The prob. of being in counterfactual sample*/
sca educ_de_2=number_counterfactual/(number_factual+number_counterfactual)


*weights
mat educ_w_couples=J(4,4,.)

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat educ_w_couples[`i',`j']=(educ_num_1_couples[`i',`j']/educ_de_1_couples[`i',`j'])*(educ_num_2/educ_de_2)
	}
}

mat educ_w_single_m=J(4,1,.)

forvalues i=1(1)4{
		mat educ_w_single_m[`i',1]=(educ_num_1_single_m[`i',1]/educ_de_1_single_m[`i',1])*(educ_num_2/educ_de_2)
}

mat educ_w_single_f=J(1,4,.)

forvalues i=1(1)4{
		mat educ_w_single_f[1,`i']=(educ_num_1_single_f[1,`i']/educ_de_1_single_f[1,`i'])*(educ_num_2/educ_de_2)
}

gen weights=.
forvalues i=1(1)4{
	forvalues j=1(1)4{
replace weights=educ_w_couples[`i',`j'] if educ_male==`i' & educ_female==`j' & relationship==1
	}
}

forvalues i=1(1)4{
	replace weights=educ_w_single_m[`i',1] if relationship==0 & koen=="1" & fined==`i'
}

forvalues i=1(1)4{
	replace weights=educ_w_single_f[1,`i'] if relationship==0 & koen=="2" & fined==`i'
}

replace weights=1 if weights==. /*Then those with missing type are counted same way in factual and counterfactual scenario*/

*Inequality 
forvalues y=2018(1)2018{
qui ineqdeco household_earnings [weight=weights] if aar==`y'

sca gini_cf_mar_educ_`y'=r(gini)
sca p90p50_cf_mar_educ_`y'=r(p90p50)
sca p50p10_cf_mar_educ_`y'=r(p50)/r(p10)
}


frame change results

gen gini_cf_mar_educ=.
forvalues i=2018(1)2018{
replace gini_cf_mar_educ=gini_cf_mar_educ_`i' if aar==`i' 
}

gen p90p50_cf_mar_educ=.
forvalues i=2018(1)2018{
replace p90p50_cf_mar_educ=p90p50_cf_mar_educ_`i' if aar==`i' 
}

gen p50p10_cf_mar_educ=.
forvalues i=2018(1)2018{
replace p50p10_cf_mar_educ=p50p10_cf_mar_educ_`i' if aar==`i' 
}

frame change default


drop weights


*obtain weights - field

*numerator 1
mat field_num_1_couples=J(8,8,.) /*The prob. of counterfactual given couple cell x,x*/

forvalues i=1(1)8{
	forvalues j=1(1)8{
		mat field_num_1_couples[`i',`j']=field_cf_mar_2018[`i',`j']/(field_cf_mar_2018[`i',`j']+field_2018[`i',`j'])
	}
}

mat field_num_1_single_m=J(8,1,.) /*The prob. of counterfactual given single men cell x*/

forvalues i=1(1)8{
	mat field_num_1_single_m[`i',1]=field_single_m_cf_mar_2018[`i',1]/(field_single_m_cf_mar_2018[`i',1]+field_single_m_2018[`i',1])
}

mat field_num_1_single_f=J(1,8,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)8{
	mat field_num_1_single_f[1,`i']=field_single_f_cf_mar_2018[1,`i']/(field_single_f_cf_mar_2018[1,`i']+field_single_f_2018[1,`i'])
}

*denominator 1
mat field_de_1_couples=J(8,8,.) /*The prob. of factual given couple cell x,x*/

forvalues i=1(1)8{
	forvalues j=1(1)8{
		mat field_de_1_couples[`i',`j']=field_2018[`i',`j']/(field_cf_mar_2018[`i',`j']+field_2018[`i',`j'])
	}
}

mat field_de_1_single_m=J(8,1,.) /*The prob. of factual given single men cell x*/

forvalues i=1(1)8{
	mat field_de_1_single_m[`i',1]=field_single_m_2018[`i',1]/(field_single_m_cf_mar_2018[`i',1]+field_single_m_2018[`i',1])
}

mat field_de_1_single_f=J(1,8,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)8{
	mat field_de_1_single_f[1,`i']=field_single_f_2018[1,`i']/(field_single_f_cf_mar_2018[1,`i']+field_single_f_2018[1,`i'])
}

*numerator 2 /*The prob. of being in factual sample*/

sca number_factual=field_2018[1,1]+field_2018[1,2]+field_2018[1,3]+field_2018[1,4]+field_2018[1,5]+field_2018[1,6]+field_2018[1,7]+field_2018[1,8] ///
	+field_2018[2,1]+field_2018[2,2]+field_2018[2,3]+field_2018[2,4]+field_2018[2,5]+field_2018[2,6]+field_2018[2,7]+field_2018[2,8] ///
	+field_2018[3,1]+field_2018[3,2]+field_2018[3,3]+field_2018[3,4]+field_2018[3,5]+field_2018[3,6]+field_2018[3,7]+field_2018[3,8] ///
	+field_2018[4,1]+field_2018[4,2]+field_2018[4,3]+field_2018[4,4]+field_2018[4,5]+field_2018[4,6]+field_2018[4,7]+field_2018[4,8] ///
	+field_2018[5,1]+field_2018[5,2]+field_2018[5,3]+field_2018[5,4]+field_2018[5,5]+field_2018[5,6]+field_2018[5,7]+field_2018[5,8] ///
	+field_2018[6,1]+field_2018[6,2]+field_2018[6,3]+field_2018[6,4]+field_2018[6,5]+field_2018[6,6]+field_2018[6,7]+field_2018[6,8] ///
	+field_2018[7,1]+field_2018[7,2]+field_2018[7,3]+field_2018[7,4]+field_2018[7,5]+field_2018[7,6]+field_2018[7,7]+field_2018[7,8] ///
	+field_2018[8,1]+field_2018[8,2]+field_2018[8,3]+field_2018[8,4]+field_2018[8,5]+field_2018[8,6]+field_2018[8,7]+field_2018[8,8] ///
	+field_single_m_2018[1,1]+field_single_m_2018[2,1]+field_single_m_2018[3,1]+field_single_m_2018[4,1]+field_single_m_2018[5,1]+field_single_m_2018[6,1]+field_single_m_2018[7,1]+field_single_m_2018[8,1] ///
	+field_single_f_2018[1,1]+field_single_f_2018[1,2]+field_single_f_2018[1,3]+field_single_f_2018[1,4]+field_single_f_2018[1,5]+field_single_f_2018[1,6]+field_single_f_2018[1,7]+field_single_f_2018[1,8]
	
sca number_factual=field_cf_mar_2018[1,1]+field_cf_mar_2018[1,2]+field_cf_mar_2018[1,3]+field_cf_mar_2018[1,4]+field_cf_mar_2018[1,5]+field_cf_mar_2018[1,6]+field_cf_mar_2018[1,7]+field_cf_mar_2018[1,8] ///
	+field_cf_mar_2018[2,1]+field_cf_mar_2018[2,2]+field_cf_mar_2018[2,3]+field_cf_mar_2018[2,4]+field_cf_mar_2018[2,5]+field_cf_mar_2018[2,6]+field_cf_mar_2018[2,7]+field_cf_mar_2018[2,8] ///
	+field_cf_mar_2018[3,1]+field_cf_mar_2018[3,2]+field_cf_mar_2018[3,3]+field_cf_mar_2018[3,4]+field_cf_mar_2018[3,5]+field_cf_mar_2018[3,6]+field_cf_mar_2018[3,7]+field_cf_mar_2018[3,8] ///
	+field_cf_mar_2018[4,1]+field_cf_mar_2018[4,2]+field_cf_mar_2018[4,3]+field_cf_mar_2018[4,4]+field_cf_mar_2018[4,5]+field_cf_mar_2018[4,6]+field_cf_mar_2018[4,7]+field_cf_mar_2018[4,8] ///
	+field_cf_mar_2018[5,1]+field_cf_mar_2018[5,2]+field_cf_mar_2018[5,3]+field_cf_mar_2018[5,4]+field_cf_mar_2018[5,5]+field_cf_mar_2018[5,6]+field_cf_mar_2018[5,7]+field_cf_mar_2018[5,8] ///
	+field_cf_mar_2018[6,1]+field_cf_mar_2018[6,2]+field_cf_mar_2018[6,3]+field_cf_mar_2018[6,4]+field_cf_mar_2018[6,5]+field_cf_mar_2018[6,6]+field_cf_mar_2018[6,7]+field_cf_mar_2018[6,8] ///
	+field_cf_mar_2018[7,1]+field_cf_mar_2018[7,2]+field_cf_mar_2018[7,3]+field_cf_mar_2018[7,4]+field_cf_mar_2018[7,5]+field_cf_mar_2018[7,6]+field_cf_mar_2018[7,7]+field_cf_mar_2018[7,8] ///
	+field_cf_mar_2018[8,1]+field_cf_mar_2018[8,2]+field_cf_mar_2018[8,3]+field_cf_mar_2018[8,4]+field_cf_mar_2018[8,5]+field_cf_mar_2018[8,6]+field_cf_mar_2018[8,7]+field_cf_mar_2018[8,8] ///
	+field_single_m_cf_mar_2018[1,1]+field_single_m_cf_mar_2018[2,1]+field_single_m_cf_mar_2018[3,1]+field_single_m_cf_mar_2018[4,1]+field_single_m_cf_mar_2018[5,1]+field_single_m_cf_mar_2018[6,1]+field_single_m_cf_mar_2018[7,1]+field_single_m_cf_mar_2018[8,1] ///
	+field_single_f_cf_mar_2018[1,1]+field_single_f_cf_mar_2018[1,2]+field_single_f_cf_mar_2018[1,3]+field_single_f_cf_mar_2018[1,4]+field_single_f_cf_mar_2018[1,5]+field_single_f_cf_mar_2018[1,6]+field_single_f_cf_mar_2018[1,7]+field_single_f_cf_mar_2018[1,8]

	
sca field_num_2=number_factual/(number_factual+number_counterfactual)

*denominator 2 /*The prob. of being in counterfactual sample*/
sca field_de_2=number_counterfactual/(number_factual+number_counterfactual)


*weights
mat field_w_couples=J(8,8,.)

forvalues i=1(1)8{
	forvalues j=1(1)8{
		mat field_w_couples[`i',`j']=(field_num_1_couples[`i',`j']/field_de_1_couples[`i',`j'])*(field_num_2/field_de_2)
	}
}

mat field_w_single_m=J(8,1,.)

forvalues i=1(1)8{
		mat field_w_single_m[`i',1]=(field_num_1_single_m[`i',1]/field_de_1_single_m[`i',1])*(field_num_2/field_de_2)
}

mat field_w_single_f=J(1,8,.)

forvalues i=1(1)8{
		mat field_w_single_f[1,`i']=(field_num_1_single_f[1,`i']/field_de_1_single_f[1,`i'])*(field_num_2/field_de_2)
}

gen weights=.
forvalues i=1(1)8{
	forvalues j=1(1)8{
replace weights=field_w_couples[`i',`j'] if field_male==`i' & field_female==`j' & relationship==1
	}
}

forvalues i=1(1)8{
	replace weights=field_w_single_m[`i',1] if relationship==0 & koen=="1" & educ_level_field_num==`i'
}

forvalues i=1(1)8{
	replace weights=field_w_single_f[1,`i'] if relationship==0 & koen=="2" & educ_level_field_num==`i'
}

replace weights=1 if weights==. /*Then those with missing type are counted same way in factual and counterfactual scenario*/

*Inequality 
forvalues y=2018(1)2018{
qui ineqdeco household_earnings [weight=weights] if aar==`y'

sca gini_cf_mar_field_`y'=r(gini)
sca p90p50_cf_mar_field_`y'=r(p90p50)
sca p50p10_cf_mar_field_`y'=r(p50)/r(p10)
}


frame change results

gen gini_cf_mar_field=.
forvalues i=2018(1)2018{
replace gini_cf_mar_field=gini_cf_mar_field_`i' if aar==`i' 
}

gen p90p50_cf_mar_field=.
forvalues i=2018(1)2018{
replace p90p50_cf_mar_field=p90p50_cf_mar_field_`i' if aar==`i' 
}

gen p50p10_cf_mar_field=.
forvalues i=2018(1)2018{
replace p50p10_cf_mar_field=p50p10_cf_mar_field_`i' if aar==`i' 
}

frame change default



drop weights

}



*Fixed marginals - male only*
{
*Solve for 2018 single numbers with 2018 preferences and 1980 marginals (male)*

*ambition types

program nlfaq

syntax varlist(min=1 max=1) [if], at(name)

tempname m1 m2 m3 m4 f1 f2 f3 f4

scalar `m1' = `at'[1,1]
scalar `m2' = `at'[1,2]
scalar `m3' = `at'[1,3]
scalar `m4' = `at'[1,4]
scalar `f1' = `at'[1,5]
scalar `f2' = `at'[1,6]
scalar `f3' = `at'[1,7]
scalar `f4' = `at'[1,8]

tempvar yh
gen double `yh'= am_pi_2018[1,1]*((`m1'*`f1')^(1/2))*((am_mar_m_base[1,1]*am_mar_f_2018[1,1])^((1/2)))+am_pi_2018[1,2]*((`m1'*`f2')^(1/2))*(((am_mar_m_base[1,1]*am_mar_f_2018[1,2]))^((1/2)))+am_pi_2018[1,3]*((`m1'*`f3')^(1/2))*(((am_mar_m_base[1,1]*am_mar_f_2018[1,3]))^((1/2)))+am_pi_2018[1,4]*((`m1'*`f4')^(1/2))*(((am_mar_m_base[1,1]*am_mar_f_2018[1,4]))^((1/2)))+`m1'-am_mar_m_base[1,1]+1 in 1

replace `yh'= am_pi_2018[2,1]*((`m2'*`f1')^(1/2))*(((am_mar_m_base[2,1]*am_mar_f_2018[1,1]))^((1/2)))+am_pi_2018[2,2]*((`m2'*`f2')^(1/2))*(((am_mar_m_base[2,1]*am_mar_f_2018[1,2]))^((1/2)))+am_pi_2018[2,3]*((`m2'*`f3')^(1/2))*(((am_mar_m_base[2,1]*am_mar_f_2018[1,3]))^((1/2)))+am_pi_2018[2,4]*((`m2'*`f4')^(1/2))*(((am_mar_m_base[2,1]*am_mar_f_2018[1,4]))^((1/2)))+`m2'-am_mar_m_base[2,1]+1 in 2

replace `yh'= am_pi_2018[3,1]*((`m3'*`f1')^(1/2))*(((am_mar_m_base[3,1]*am_mar_f_2018[1,1]))^((1/2)))+am_pi_2018[3,2]*((`m3'*`f2')^(1/2))*(((am_mar_m_base[3,1]*am_mar_f_2018[1,2]))^((1/2)))+am_pi_2018[3,3]*((`m3'*`f3')^(1/2))*(((am_mar_m_base[3,1]*am_mar_f_2018[1,3]))^((1/2)))+am_pi_2018[3,4]*((`m3'*`f4')^(1/2))*(((am_mar_m_base[3,1]*am_mar_f_2018[1,4]))^((1/2)))+`m3'-am_mar_m_base[3,1]+1 in 3

replace `yh'= am_pi_2018[4,1]*((`m4'*`f1')^(1/2))*(((am_mar_m_base[4,1]*am_mar_f_2018[1,1]))^((1/2)))+am_pi_2018[4,2]*((`m4'*`f2')^(1/2))*(((am_mar_m_base[4,1]*am_mar_f_2018[1,2]))^((1/2)))+am_pi_2018[4,3]*((`m4'*`f3')^(1/2))*(((am_mar_m_base[4,1]*am_mar_f_2018[1,3]))^((1/2)))+am_pi_2018[4,4]*((`m4'*`f4')^(1/2))*(((am_mar_m_base[4,1]*am_mar_f_2018[1,4]))^((1/2)))+`m4'-am_mar_m_base[4,1]+1 in 4

replace `yh'=am_pi_2018[1,1]*((`m1'*`f1')^(1/2))*(((am_mar_m_base[1,1]*am_mar_f_2018[1,1]))^((1/2)))+am_pi_2018[2,1]*((`m2'*`f1')^(1/2))*(((am_mar_m_base[2,1]*am_mar_f_2018[1,1]))^((1/2)))+am_pi_2018[3,1]*((`m3'*`f1')^(1/2))*(((am_mar_m_base[3,1]*am_mar_f_2018[1,1]))^((1/2)))+am_pi_2018[4,1]*((`m4'*`f1')^(1/2))*(((am_mar_m_base[4,1]*am_mar_f_2018[1,1]))^((1/2)))+`f1'-am_mar_f_2018[1,1] in 5

replace `yh'=am_pi_2018[1,2]*((`m1'*`f2')^(1/2))*(((am_mar_m_base[1,1]*am_mar_f_2018[1,2]))^((1/2)))+am_pi_2018[2,2]*((`m2'*`f2')^(1/2))*(((am_mar_m_base[2,1]*am_mar_f_2018[1,2]))^((1/2)))+am_pi_2018[3,2]*((`m3'*`f2')^(1/2))*(((am_mar_m_base[3,1]*am_mar_f_2018[1,2]))^((1/2)))+am_pi_2018[4,2]*((`m4'*`f2')^(1/2))*(((am_mar_m_base[4,1]*am_mar_f_2018[1,2]))^((1/2)))+`f2'-am_mar_f_2018[1,2] in 6

replace `yh'=am_pi_2018[1,3]*((`m1'*`f3')^(1/2))*(((am_mar_m_base[1,1]*am_mar_f_2018[1,3]))^((1/2)))+am_pi_2018[2,3]*((`m2'*`f3')^(1/2))*(((am_mar_m_base[2,1]*am_mar_f_2018[1,3]))^((1/2)))+am_pi_2018[3,3]*((`m3'*`f3')^(1/2))*(((am_mar_m_base[3,1]*am_mar_f_2018[1,3]))^((1/2)))+am_pi_2018[4,3]*((`m4'*`f3')^(1/2))*(((am_mar_m_base[4,1]*am_mar_f_2018[1,3]))^((1/2)))+`f3'-am_mar_f_2018[1,3] in 7

replace `yh'=am_pi_2018[1,4]*((`m1'*`f4')^(1/2))*(((am_mar_m_base[1,1]*am_mar_f_2018[1,4]))^((1/2)))+am_pi_2018[2,4]*((`m2'*`f4')^(1/2))*(((am_mar_m_base[2,1]*am_mar_f_2018[1,4]))^((1/2)))+am_pi_2018[3,4]*((`m3'*`f4')^(1/2))*(((am_mar_m_base[3,1]*am_mar_f_2018[1,4]))^((1/2)))+am_pi_2018[4,4]*((`m4'*`f4')^(1/2))*(((am_mar_m_base[4,1]*am_mar_f_2018[1,4]))^((1/2)))+`f4'-am_mar_f_2018[1,4] in 8



replace `varlist'=`yh'
end


frame create temp
frame change temp

clear
set obs 8

gen y = 0
replace y=1 in 1

nl faq @ y, parameters(m1 m2 m3 m4 f1 f2 f3 f4) initial(m1 1 m2 1 m3 1 m4 1 f1 1 f2 1 f3 1 f4 1)

*get counter factual singles
mat am_single_m_cf_marm_2018=J(4,1,.)
mat am_single_m_cf_marm_2018[1,1]=[m1]_b[_cons]
mat am_single_m_cf_marm_2018[2,1]=[m2]_b[_cons]
mat am_single_m_cf_marm_2018[3,1]=[m3]_b[_cons]
mat am_single_m_cf_marm_2018[4,1]=[m4]_b[_cons]

mat am_single_f_cf_marm_2018=J(1,4,.)
mat am_single_f_cf_marm_2018[1,1]=[f1]_b[_cons]
mat am_single_f_cf_marm_2018[1,2]=[f2]_b[_cons]
mat am_single_f_cf_marm_2018[1,3]=[f3]_b[_cons]
mat am_single_f_cf_marm_2018[1,4]=[f4]_b[_cons]

frame change default
frame drop temp
prog drop nlfaq




*education types
program nlfaq

syntax varlist(min=1 max=1) [if], at(name)

tempname m1 m2 m3 m4 f1 f2 f3 f4

scalar `m1' = `at'[1,1]
scalar `m2' = `at'[1,2]
scalar `m3' = `at'[1,3]
scalar `m4' = `at'[1,4]
scalar `f1' = `at'[1,5]
scalar `f2' = `at'[1,6]
scalar `f3' = `at'[1,7]
scalar `f4' = `at'[1,8]

tempvar yh
gen double `yh'= educ_pi_2018[1,1]*((`m1'*`f1')^(1/2))*((educ_mar_m_base[1,1]*educ_mar_f_2018[1,1])^((1/2)))+educ_pi_2018[1,2]*((`m1'*`f2')^(1/2))*(((educ_mar_m_base[1,1]*educ_mar_f_2018[1,2]))^((1/2)))+educ_pi_2018[1,3]*((`m1'*`f3')^(1/2))*(((educ_mar_m_base[1,1]*educ_mar_f_2018[1,3]))^((1/2)))+educ_pi_2018[1,4]*((`m1'*`f4')^(1/2))*(((educ_mar_m_base[1,1]*educ_mar_f_2018[1,4]))^((1/2)))+`m1'-educ_mar_m_base[1,1]+1 in 1

replace `yh'= educ_pi_2018[2,1]*((`m2'*`f1')^(1/2))*(((educ_mar_m_base[2,1]*educ_mar_f_2018[1,1]))^((1/2)))+educ_pi_2018[2,2]*((`m2'*`f2')^(1/2))*(((educ_mar_m_base[2,1]*educ_mar_f_2018[1,2]))^((1/2)))+educ_pi_2018[2,3]*((`m2'*`f3')^(1/2))*(((educ_mar_m_base[2,1]*educ_mar_f_2018[1,3]))^((1/2)))+educ_pi_2018[2,4]*((`m2'*`f4')^(1/2))*(((educ_mar_m_base[2,1]*educ_mar_f_2018[1,4]))^((1/2)))+`m2'-educ_mar_m_base[2,1]+1 in 2

replace `yh'= educ_pi_2018[3,1]*((`m3'*`f1')^(1/2))*(((educ_mar_m_base[3,1]*educ_mar_f_2018[1,1]))^((1/2)))+educ_pi_2018[3,2]*((`m3'*`f2')^(1/2))*(((educ_mar_m_base[3,1]*educ_mar_f_2018[1,2]))^((1/2)))+educ_pi_2018[3,3]*((`m3'*`f3')^(1/2))*(((educ_mar_m_base[3,1]*educ_mar_f_2018[1,3]))^((1/2)))+educ_pi_2018[3,4]*((`m3'*`f4')^(1/2))*(((educ_mar_m_base[3,1]*educ_mar_f_2018[1,4]))^((1/2)))+`m3'-educ_mar_m_base[3,1]+1 in 3

replace `yh'= educ_pi_2018[4,1]*((`m4'*`f1')^(1/2))*(((educ_mar_m_base[4,1]*educ_mar_f_2018[1,1]))^((1/2)))+educ_pi_2018[4,2]*((`m4'*`f2')^(1/2))*(((educ_mar_m_base[4,1]*educ_mar_f_2018[1,2]))^((1/2)))+educ_pi_2018[4,3]*((`m4'*`f3')^(1/2))*(((educ_mar_m_base[4,1]*educ_mar_f_2018[1,3]))^((1/2)))+educ_pi_2018[4,4]*((`m4'*`f4')^(1/2))*(((educ_mar_m_base[4,1]*educ_mar_f_2018[1,4]))^((1/2)))+`m4'-educ_mar_m_base[4,1]+1 in 4

replace `yh'=educ_pi_2018[1,1]*((`m1'*`f1')^(1/2))*(((educ_mar_m_base[1,1]*educ_mar_f_2018[1,1]))^((1/2)))+educ_pi_2018[2,1]*((`m2'*`f1')^(1/2))*(((educ_mar_m_base[2,1]*educ_mar_f_2018[1,1]))^((1/2)))+educ_pi_2018[3,1]*((`m3'*`f1')^(1/2))*(((educ_mar_m_base[3,1]*educ_mar_f_2018[1,1]))^((1/2)))+educ_pi_2018[4,1]*((`m4'*`f1')^(1/2))*(((educ_mar_m_base[4,1]*educ_mar_f_2018[1,1]))^((1/2)))+`f1'-educ_mar_f_2018[1,1] in 5

replace `yh'=educ_pi_2018[1,2]*((`m1'*`f2')^(1/2))*(((educ_mar_m_base[1,1]*educ_mar_f_2018[1,2]))^((1/2)))+educ_pi_2018[2,2]*((`m2'*`f2')^(1/2))*(((educ_mar_m_base[2,1]*educ_mar_f_2018[1,2]))^((1/2)))+educ_pi_2018[3,2]*((`m3'*`f2')^(1/2))*(((educ_mar_m_base[3,1]*educ_mar_f_2018[1,2]))^((1/2)))+educ_pi_2018[4,2]*((`m4'*`f2')^(1/2))*(((educ_mar_m_base[4,1]*educ_mar_f_2018[1,2]))^((1/2)))+`f2'-educ_mar_f_2018[1,2] in 6

replace `yh'=educ_pi_2018[1,3]*((`m1'*`f3')^(1/2))*(((educ_mar_m_base[1,1]*educ_mar_f_2018[1,3]))^((1/2)))+educ_pi_2018[2,3]*((`m2'*`f3')^(1/2))*(((educ_mar_m_base[2,1]*educ_mar_f_2018[1,3]))^((1/2)))+educ_pi_2018[3,3]*((`m3'*`f3')^(1/2))*(((educ_mar_m_base[3,1]*educ_mar_f_2018[1,3]))^((1/2)))+educ_pi_2018[4,3]*((`m4'*`f3')^(1/2))*(((educ_mar_m_base[4,1]*educ_mar_f_2018[1,3]))^((1/2)))+`f3'-educ_mar_f_2018[1,3] in 7

replace `yh'=educ_pi_2018[1,4]*((`m1'*`f4')^(1/2))*(((educ_mar_m_base[1,1]*educ_mar_f_2018[1,4]))^((1/2)))+educ_pi_2018[2,4]*((`m2'*`f4')^(1/2))*(((educ_mar_m_base[2,1]*educ_mar_f_2018[1,4]))^((1/2)))+educ_pi_2018[3,4]*((`m3'*`f4')^(1/2))*(((educ_mar_m_base[3,1]*educ_mar_f_2018[1,4]))^((1/2)))+educ_pi_2018[4,4]*((`m4'*`f4')^(1/2))*(((educ_mar_m_base[4,1]*educ_mar_f_2018[1,4]))^((1/2)))+`f4'-educ_mar_f_2018[1,4] in 8


replace `varlist'=`yh'
end


frame create temp
frame change temp

clear
set obs 8

gen y = 0
replace y=1 in 1

nl faq @ y, parameters(m1 m2 m3 m4 f1 f2 f3 f4) initial(m1 1 m2 1 m3 1 m4 1 f1 1 f2 1 f3 1 f4 1)

*get counter factual singles
mat educ_single_m_cf_marm_2018=J(4,1,.)
mat educ_single_m_cf_marm_2018[1,1]=[m1]_b[_cons]
mat educ_single_m_cf_marm_2018[2,1]=[m2]_b[_cons]
mat educ_single_m_cf_marm_2018[3,1]=[m3]_b[_cons]
mat educ_single_m_cf_marm_2018[4,1]=[m4]_b[_cons]

mat educ_single_f_cf_marm_2018=J(1,4,.)
mat educ_single_f_cf_marm_2018[1,1]=[f1]_b[_cons]
mat educ_single_f_cf_marm_2018[1,2]=[f2]_b[_cons]
mat educ_single_f_cf_marm_2018[1,3]=[f3]_b[_cons]
mat educ_single_f_cf_marm_2018[1,4]=[f4]_b[_cons]


frame change default
frame drop temp
prog drop nlfaq

*field types
program nlfaq

syntax varlist(min=1 max=1) [if], at(name)

tempname m1 m2 m3 m4 m5 m6 m7 m8 f1 f2 f3 f4 f5 f6 f7 f8

scalar `m1' = `at'[1,1]
scalar `m2' = `at'[1,2]
scalar `m3' = `at'[1,3]
scalar `m4' = `at'[1,4]
scalar `m5' = `at'[1,5]
scalar `m6' = `at'[1,6]
scalar `m7' = `at'[1,7]
scalar `m8' = `at'[1,8]
scalar `f1' = `at'[1,9]
scalar `f2' = `at'[1,10]
scalar `f3' = `at'[1,11]
scalar `f4' = `at'[1,12]
scalar `f5' = `at'[1,13]
scalar `f6' = `at'[1,14]
scalar `f7' = `at'[1,15]
scalar `f8' = `at'[1,16]

tempvar yh
gen double `yh'= field_pi_2018[1,1]*((`m1'*`f1')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_2018[1,2]*((`m1'*`f2')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_2018[1,3]*((`m1'*`f3')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_2018[1,4]*((`m1'*`f4')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_2018[1,5]*((`m1'*`f5')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_2018[1,6]*((`m1'*`f6')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_2018[1,7]*((`m1'*`f7')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_2018[1,8]*((`m1'*`f8')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_2018[1,8]))^((1/2)))+`m1'-field_mar_m_base[1,1]+1 in 1

replace `yh'= field_pi_2018[2,1]*((`m2'*`f1')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_2018[2,2]*((`m2'*`f2')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_2018[2,3]*((`m2'*`f3')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_2018[2,4]*((`m2'*`f4')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_2018[2,5]*((`m2'*`f5')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_2018[2,6]*((`m2'*`f6')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_2018[2,7]*((`m2'*`f7')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_2018[2,8]*((`m2'*`f8')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_2018[1,8]))^((1/2)))+`m2'-field_mar_m_base[2,1]+1 in 2

replace `yh'= field_pi_2018[3,1]*((`m3'*`f1')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_2018[3,2]*((`m3'*`f2')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_2018[3,3]*((`m3'*`f3')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_2018[3,4]*((`m3'*`f4')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_2018[3,5]*((`m3'*`f5')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_2018[3,6]*((`m3'*`f6')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_2018[3,7]*((`m3'*`f7')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_2018[3,8]*((`m3'*`f8')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_2018[1,8]))^((1/2)))+`m3'-field_mar_m_base[3,1]+1 in 3

replace `yh'= field_pi_2018[4,1]*((`m4'*`f1')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_2018[4,2]*((`m4'*`f2')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_2018[4,3]*((`m4'*`f3')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_2018[4,4]*((`m4'*`f4')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_2018[4,5]*((`m4'*`f5')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_2018[4,6]*((`m4'*`f6')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_2018[4,7]*((`m4'*`f7')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_2018[4,8]*((`m4'*`f8')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_2018[1,8]))^((1/2)))+`m4'-field_mar_m_base[4,1]+1 in 4

replace `yh'= field_pi_2018[5,1]*((`m5'*`f1')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_2018[5,2]*((`m5'*`f2')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_2018[5,3]*((`m5'*`f3')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_2018[5,4]*((`m5'*`f4')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_2018[5,5]*((`m5'*`f5')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_2018[5,6]*((`m5'*`f6')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_2018[5,7]*((`m5'*`f7')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_2018[5,8]*((`m5'*`f8')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_2018[1,8]))^((1/2)))+`m5'-field_mar_m_base[5,1]+1 in 5

replace `yh'= field_pi_2018[6,1]*((`m6'*`f1')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_2018[6,2]*((`m6'*`f2')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_2018[6,3]*((`m6'*`f3')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_2018[6,4]*((`m6'*`f4')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_2018[6,5]*((`m6'*`f5')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_2018[6,6]*((`m6'*`f6')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_2018[6,7]*((`m6'*`f7')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_2018[6,8]*((`m6'*`f8')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_2018[1,8]))^((1/2)))+`m6'-field_mar_m_base[6,1]+1 in 6

replace `yh'= field_pi_2018[7,1]*((`m7'*`f1')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_2018[7,2]*((`m7'*`f2')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_2018[7,3]*((`m7'*`f3')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_2018[7,4]*((`m7'*`f4')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_2018[7,5]*((`m7'*`f5')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_2018[7,6]*((`m7'*`f6')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_2018[7,7]*((`m7'*`f7')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_2018[7,8]*((`m7'*`f8')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_2018[1,8]))^((1/2)))+`m7'-field_mar_m_base[7,1]+1 in 7

replace `yh'= field_pi_2018[8,1]*((`m8'*`f1')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_2018[8,2]*((`m8'*`f2')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_2018[8,3]*((`m8'*`f3')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_2018[8,4]*((`m8'*`f4')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_2018[8,5]*((`m8'*`f5')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_2018[8,6]*((`m8'*`f6')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_2018[8,7]*((`m8'*`f7')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_2018[8,8]*((`m8'*`f8')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_2018[1,8]))^((1/2)))+`m8'-field_mar_m_base[8,1]+1 in 8

replace `yh'=field_pi_2018[1,1]*((`m1'*`f1')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_2018[2,1]*((`m2'*`f1')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_2018[3,1]*((`m3'*`f1')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_2018[4,1]*((`m4'*`f1')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_2018[5,1]*((`m5'*`f1')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_2018[6,1]*((`m6'*`f1')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_2018[7,1]*((`m7'*`f1')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_2018[1,1]))^((1/2)))+field_pi_2018[8,1]*((`m8'*`f1')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_2018[1,1]))^((1/2)))+`f1'-field_mar_f_2018[1,1] in 9

replace `yh'=field_pi_2018[1,2]*((`m1'*`f2')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_2018[2,2]*((`m2'*`f2')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_2018[3,2]*((`m3'*`f2')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_2018[4,2]*((`m4'*`f2')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_2018[5,2]*((`m5'*`f2')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_2018[6,2]*((`m6'*`f2')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_2018[7,2]*((`m7'*`f2')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_2018[1,2]))^((1/2)))+field_pi_2018[8,2]*((`m8'*`f2')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_2018[1,2]))^((1/2)))+`f2'-field_mar_f_2018[1,2] in 10

replace `yh'=field_pi_2018[1,3]*((`m1'*`f3')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_2018[2,3]*((`m2'*`f3')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_2018[3,3]*((`m3'*`f3')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_2018[4,3]*((`m4'*`f3')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_2018[5,3]*((`m5'*`f3')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_2018[6,3]*((`m6'*`f3')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_2018[7,3]*((`m7'*`f3')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_2018[1,3]))^((1/2)))+field_pi_2018[8,3]*((`m8'*`f3')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_2018[1,3]))^((1/2)))+`f3'-field_mar_f_2018[1,3] in 11

replace `yh'=field_pi_2018[1,4]*((`m1'*`f4')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_2018[2,4]*((`m2'*`f4')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_2018[3,4]*((`m3'*`f4')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_2018[4,4]*((`m4'*`f4')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_2018[5,4]*((`m5'*`f4')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_2018[6,4]*((`m6'*`f4')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_2018[7,4]*((`m7'*`f4')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_2018[1,4]))^((1/2)))+field_pi_2018[8,4]*((`m8'*`f4')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_2018[1,4]))^((1/2)))+`f4'-field_mar_f_2018[1,4] in 12

replace `yh'=field_pi_2018[1,5]*((`m1'*`f5')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_2018[2,5]*((`m2'*`f5')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_2018[3,5]*((`m3'*`f5')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_2018[4,5]*((`m4'*`f5')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_2018[5,5]*((`m5'*`f5')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_2018[6,5]*((`m6'*`f5')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_2018[7,5]*((`m7'*`f5')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_2018[1,5]))^((1/2)))+field_pi_2018[8,5]*((`m8'*`f5')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_2018[1,5]))^((1/2)))+`f5'-field_mar_f_2018[1,5] in 13

replace `yh'=field_pi_2018[1,6]*((`m1'*`f6')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_2018[2,6]*((`m2'*`f6')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_2018[3,6]*((`m3'*`f6')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_2018[4,6]*((`m4'*`f6')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_2018[5,6]*((`m5'*`f6')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_2018[6,6]*((`m6'*`f6')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_2018[7,6]*((`m7'*`f6')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_2018[1,6]))^((1/2)))+field_pi_2018[8,6]*((`m8'*`f6')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_2018[1,6]))^((1/2)))+`f6'-field_mar_f_2018[1,6] in 14

replace `yh'=field_pi_2018[1,7]*((`m1'*`f7')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_2018[2,7]*((`m2'*`f7')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_2018[3,7]*((`m3'*`f7')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_2018[4,7]*((`m4'*`f7')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_2018[5,7]*((`m5'*`f7')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_2018[6,7]*((`m6'*`f7')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_2018[7,7]*((`m7'*`f7')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_2018[1,7]))^((1/2)))+field_pi_2018[8,7]*((`m8'*`f7')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_2018[1,7]))^((1/2)))+`f7'-field_mar_f_2018[1,7] in 15

replace `yh'=field_pi_2018[1,8]*((`m1'*`f8')^(1/2))*(((field_mar_m_base[1,1]*field_mar_f_2018[1,8]))^((1/2)))+field_pi_2018[2,8]*((`m2'*`f8')^(1/2))*(((field_mar_m_base[2,1]*field_mar_f_2018[1,8]))^((1/2)))+field_pi_2018[3,8]*((`m3'*`f8')^(1/2))*(((field_mar_m_base[3,1]*field_mar_f_2018[1,8]))^((1/2)))+field_pi_2018[4,8]*((`m4'*`f8')^(1/2))*(((field_mar_m_base[4,1]*field_mar_f_2018[1,8]))^((1/2)))+field_pi_2018[5,8]*((`m5'*`f8')^(1/2))*(((field_mar_m_base[5,1]*field_mar_f_2018[1,8]))^((1/2)))+field_pi_2018[6,8]*((`m6'*`f8')^(1/2))*(((field_mar_m_base[6,1]*field_mar_f_2018[1,8]))^((1/2)))+field_pi_2018[7,8]*((`m7'*`f8')^(1/2))*(((field_mar_m_base[7,1]*field_mar_f_2018[1,8]))^((1/2)))+field_pi_2018[8,8]*((`m8'*`f8')^(1/2))*(((field_mar_m_base[8,1]*field_mar_f_2018[1,8]))^((1/2)))+`f8'-field_mar_f_2018[1,8] in 16



replace `varlist'=`yh'
end


frame create temp
frame change temp

clear
set obs 16

gen y = 0
replace y=1 in 1

nl faq @ y, parameters(m1 m2 m3 m4 m5 m6 m7 m8 f1 f2 f3 f4 f5 f6 f7 f8) initial(m1 1 m2 1 m3 1 m4 1 m5 1 m6 1 m7 1 m8 1 f1 1 f2 1 f3 1 f4 1 f5 1 f6 1 f7 1 f8 1)

*get counter factual singles
mat field_single_m_cf_marm_2018=J(8,1,.)
mat field_single_m_cf_marm_2018[1,1]=[m1]_b[_cons]
mat field_single_m_cf_marm_2018[2,1]=[m2]_b[_cons]
mat field_single_m_cf_marm_2018[3,1]=[m3]_b[_cons]
mat field_single_m_cf_marm_2018[4,1]=[m4]_b[_cons]
mat field_single_m_cf_marm_2018[5,1]=[m5]_b[_cons]
mat field_single_m_cf_marm_2018[6,1]=[m6]_b[_cons]
mat field_single_m_cf_marm_2018[7,1]=[m7]_b[_cons]
mat field_single_m_cf_marm_2018[8,1]=[m8]_b[_cons]

mat field_single_f_cf_marm_2018=J(1,8,.)
mat field_single_f_cf_marm_2018[1,1]=[f1]_b[_cons]
mat field_single_f_cf_marm_2018[1,2]=[f2]_b[_cons]
mat field_single_f_cf_marm_2018[1,3]=[f3]_b[_cons]
mat field_single_f_cf_marm_2018[1,4]=[f4]_b[_cons]
mat field_single_f_cf_marm_2018[1,5]=[f5]_b[_cons]
mat field_single_f_cf_marm_2018[1,6]=[f6]_b[_cons]
mat field_single_f_cf_marm_2018[1,7]=[f7]_b[_cons]
mat field_single_f_cf_marm_2018[1,8]=[f8]_b[_cons]


frame change default
frame drop temp
prog drop nlfaq

*get counter factual couple numbers*

mat am_cf_marm_2018=J(4,4,.)

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat am_cf_marm_2018[`i',`j']=am_pi_2018[`i',`j']*((am_single_m_cf_marm_2018[`i',1]*am_single_f_cf_marm_2018[1,`j'])^(1/2))*((am_mar_m_base[`i',1]*am_mar_f_2018[1,`j'])^((1/2)))
	}
}

mat educ_cf_marm_2018=J(4,4,.)

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat educ_cf_marm_2018[`i',`j']=educ_pi_2018[`i',`j']*((educ_single_m_cf_marm_2018[`i',1]*educ_single_f_cf_marm_2018[1,`j'])^(1/2))*((educ_mar_m_base[`i',1]*educ_mar_f_2018[1,`j'])^((1/2)))
	}
}

mat field_cf_marm_2018=J(8,8,.)

forvalues i=1(1)8{
	forvalues j=1(1)8{
		mat field_cf_marm_2018[`i',`j']=field_pi_2018[`i',`j']*((field_single_m_cf_marm_2018[`i',1]*field_single_f_cf_marm_2018[1,`j'])^(1/2))*((field_mar_m_base[`i',1]*field_mar_f_2018[1,`j'])^((1/2)))
	}
}


*obtain weights - ambition

*numerator 1
mat am_num_1_couples=J(4,4,.) /*The prob. of counterfactual given couple cell x,x*/

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat am_num_1_couples[`i',`j']=am_cf_marm_2018[`i',`j']/(am_cf_marm_2018[`i',`j']+am_2018[`i',`j'])
	}
}

mat am_num_1_single_m=J(4,1,.) /*The prob. of counterfactual given single men cell x*/

forvalues i=1(1)4{
	mat am_num_1_single_m[`i',1]=am_single_m_cf_marm_2018[`i',1]/(am_single_m_cf_marm_2018[`i',1]+am_single_m_2018[`i',1])
}

mat am_num_1_single_f=J(1,4,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)4{
	mat am_num_1_single_f[1,`i']=am_single_f_cf_marm_2018[1,`i']/(am_single_f_cf_marm_2018[1,`i']+am_single_f_2018[1,`i'])
}

*denominator 1
mat am_de_1_couples=J(4,4,.) /*The prob. of factual given couple cell x,x*/

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat am_de_1_couples[`i',`j']=am_2018[`i',`j']/(am_cf_marm_2018[`i',`j']+am_2018[`i',`j'])
	}
}

mat am_de_1_single_m=J(4,1,.) /*The prob. of factual given single men cell x*/

forvalues i=1(1)4{
	mat am_de_1_single_m[`i',1]=am_single_m_2018[`i',1]/(am_single_m_cf_marm_2018[`i',1]+am_single_m_2018[`i',1])
}

mat am_de_1_single_f=J(1,4,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)4{
	mat am_de_1_single_f[1,`i']=am_single_f_2018[1,`i']/(am_single_f_cf_marm_2018[1,`i']+am_single_f_2018[1,`i'])
}

*numerator 2 /*The prob. of being in factual sample*/

sca number_factual=am_2018[1,1]+am_2018[1,2]+am_2018[1,3]+am_2018[1,4] ///
	+am_2018[2,1]+am_2018[2,2]+am_2018[2,3]+am_2018[2,4] ///
	+am_2018[3,1]+am_2018[3,2]+am_2018[3,3]+am_2018[3,4] ///
	+am_2018[4,1]+am_2018[4,2]+am_2018[4,3]+am_2018[4,4] ///
	+am_single_m_2018[1,1]+am_single_m_2018[2,1]+am_single_m_2018[3,1]+am_single_m_2018[4,1] ///
	+am_single_f_2018[1,1]+am_single_f_2018[1,2]+am_single_f_2018[1,3]+am_single_f_2018[1,4]
	
sca number_counterfactual=am_cf_marm_2018[1,1]+am_cf_marm_2018[1,2]+am_cf_marm_2018[1,3]+am_cf_marm_2018[1,4] ///
	+am_cf_marm_2018[2,1]+am_cf_marm_2018[2,2]+am_cf_marm_2018[2,3]+am_cf_marm_2018[2,4] ///
	+am_cf_marm_2018[3,1]+am_cf_marm_2018[3,2]+am_cf_marm_2018[3,3]+am_cf_marm_2018[3,4] ///
	+am_cf_marm_2018[4,1]+am_cf_marm_2018[4,2]+am_cf_marm_2018[4,3]+am_cf_marm_2018[4,4] ///
	+am_single_m_cf_marm_2018[1,1]+am_single_m_cf_marm_2018[2,1]+am_single_m_cf_marm_2018[3,1]+am_single_m_cf_marm_2018[4,1] ///
	+am_single_f_cf_marm_2018[1,1]+am_single_f_cf_marm_2018[1,2]+am_single_f_cf_marm_2018[1,3]+am_single_f_cf_marm_2018[1,4]
	
sca am_num_2=number_factual/(number_factual+number_counterfactual)

*denominator 2 /*The prob. of being in counterfactual sample*/
sca am_de_2=number_counterfactual/(number_factual+number_counterfactual)


*weights
mat am_w_couples=J(4,4,.)

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat am_w_couples[`i',`j']=(am_num_1_couples[`i',`j']/am_de_1_couples[`i',`j'])*(am_num_2/am_de_2)
	}
}

mat am_w_single_m=J(4,1,.)

forvalues i=1(1)4{
		mat am_w_single_m[`i',1]=(am_num_1_single_m[`i',1]/am_de_1_single_m[`i',1])*(am_num_2/am_de_2)
}

mat am_w_single_f=J(1,4,.)

forvalues i=1(1)4{
		mat am_w_single_f[1,`i']=(am_num_1_single_f[1,`i']/am_de_1_single_f[1,`i'])*(am_num_2/am_de_2)
}

gen weights=.
forvalues i=1(1)4{
	forvalues j=1(1)4{
replace weights=am_w_couples[`i',`j'] if am_male==`i' & am_female==`j' & relationship==1
	}
}

forvalues i=1(1)4{
	replace weights=am_w_single_m[`i',1] if relationship==0 & koen=="1" & ambition_type_k_4_s==`i'
}

forvalues i=1(1)4{
	replace weights=am_w_single_f[1,`i'] if relationship==0 & koen=="2" & ambition_type_k_4_s==`i'
}

replace weights=1 if weights==. /*Then those with missing type are counted same way in factual and counterfactual scenario*/

*Inequality 
forvalues y=2018(1)2018{
qui ineqdeco household_earnings [weight=weights] if aar==`y'

sca gini_cf_marm_`y'=r(gini)
sca p90p50_cf_marm_`y'=r(p90p50)
sca p50p10_cf_marm_`y'=r(p50)/r(p10)
}


frame change results

gen gini_cf_marm=.
forvalues i=2018(1)2018{
replace gini_cf_marm=gini_cf_marm_`i' if aar==`i' 
}

gen p90p50_cf_marm=.
forvalues i=2018(1)2018{
replace p90p50_cf_marm=p90p50_cf_marm_`i' if aar==`i' 
}

gen p50p10_cf_marm=.
forvalues i=2018(1)2018{
replace p50p10_cf_marm=p50p10_cf_marm_`i' if aar==`i' 
}

frame change default



drop weights


*obtain weights - education

*numerator 1
mat educ_num_1_couples=J(4,4,.) /*The prob. of counterfactual given couple cell x,x*/

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat educ_num_1_couples[`i',`j']=educ_cf_marm_2018[`i',`j']/(educ_cf_marm_2018[`i',`j']+educ_2018[`i',`j'])
	}
}

mat educ_num_1_single_m=J(4,1,.) /*The prob. of counterfactual given single men cell x*/

forvalues i=1(1)4{
	mat educ_num_1_single_m[`i',1]=educ_single_m_cf_marm_2018[`i',1]/(educ_single_m_cf_marm_2018[`i',1]+educ_single_m_2018[`i',1])
}

mat educ_num_1_single_f=J(1,4,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)4{
	mat educ_num_1_single_f[1,`i']=educ_single_f_cf_marm_2018[1,`i']/(educ_single_f_cf_marm_2018[1,`i']+educ_single_f_2018[1,`i'])
}

*denominator 1
mat educ_de_1_couples=J(4,4,.) /*The prob. of factual given couple cell x,x*/

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat educ_de_1_couples[`i',`j']=educ_2018[`i',`j']/(educ_cf_marm_2018[`i',`j']+educ_2018[`i',`j'])
	}
}

mat educ_de_1_single_m=J(4,1,.) /*The prob. of factual given single men cell x*/

forvalues i=1(1)4{
	mat educ_de_1_single_m[`i',1]=educ_single_m_2018[`i',1]/(educ_single_m_cf_marm_2018[`i',1]+educ_single_m_2018[`i',1])
}

mat educ_de_1_single_f=J(1,4,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)4{
	mat educ_de_1_single_f[1,`i']=educ_single_f_2018[1,`i']/(educ_single_f_cf_marm_2018[1,`i']+educ_single_f_2018[1,`i'])
}

*numerator 2 /*The prob. of being in factual sample*/

sca number_factual=educ_2018[1,1]+educ_2018[1,2]+educ_2018[1,3]+educ_2018[1,4] ///
	+educ_2018[2,1]+educ_2018[2,2]+educ_2018[2,3]+educ_2018[2,4] ///
	+educ_2018[3,1]+educ_2018[3,2]+educ_2018[3,3]+educ_2018[3,4] ///
	+educ_2018[4,1]+educ_2018[4,2]+educ_2018[4,3]+educ_2018[4,4] ///
	+educ_single_m_2018[1,1]+educ_single_m_2018[2,1]+educ_single_m_2018[3,1]+educ_single_m_2018[4,1] ///
	+educ_single_f_2018[1,1]+educ_single_f_2018[1,2]+educ_single_f_2018[1,3]+educ_single_f_2018[1,4]
	
sca number_counterfactual=educ_cf_marm_2018[1,1]+educ_cf_marm_2018[1,2]+educ_cf_marm_2018[1,3]+educ_cf_marm_2018[1,4] ///
	+educ_cf_marm_2018[2,1]+educ_cf_marm_2018[2,2]+educ_cf_marm_2018[2,3]+educ_cf_marm_2018[2,4] ///
	+educ_cf_marm_2018[3,1]+educ_cf_marm_2018[3,2]+educ_cf_marm_2018[3,3]+educ_cf_marm_2018[3,4] ///
	+educ_cf_marm_2018[4,1]+educ_cf_marm_2018[4,2]+educ_cf_marm_2018[4,3]+educ_cf_marm_2018[4,4] ///
	+educ_single_m_cf_marm_2018[1,1]+educ_single_m_cf_marm_2018[2,1]+educ_single_m_cf_marm_2018[3,1]+educ_single_m_cf_marm_2018[4,1] ///
	+educ_single_f_cf_marm_2018[1,1]+educ_single_f_cf_marm_2018[1,2]+educ_single_f_cf_marm_2018[1,3]+educ_single_f_cf_marm_2018[1,4]
	
sca educ_num_2=number_factual/(number_factual+number_counterfactual)

*denominator 2 /*The prob. of being in counterfactual sample*/
sca educ_de_2=number_counterfactual/(number_factual+number_counterfactual)


*weights
mat educ_w_couples=J(4,4,.)

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat educ_w_couples[`i',`j']=(educ_num_1_couples[`i',`j']/educ_de_1_couples[`i',`j'])*(educ_num_2/educ_de_2)
	}
}

mat educ_w_single_m=J(4,1,.)

forvalues i=1(1)4{
		mat educ_w_single_m[`i',1]=(educ_num_1_single_m[`i',1]/educ_de_1_single_m[`i',1])*(educ_num_2/educ_de_2)
}

mat educ_w_single_f=J(1,4,.)

forvalues i=1(1)4{
		mat educ_w_single_f[1,`i']=(educ_num_1_single_f[1,`i']/educ_de_1_single_f[1,`i'])*(educ_num_2/educ_de_2)
}

gen weights=.
forvalues i=1(1)4{
	forvalues j=1(1)4{
replace weights=educ_w_couples[`i',`j'] if educ_male==`i' & educ_female==`j' & relationship==1
	}
}

forvalues i=1(1)4{
	replace weights=educ_w_single_m[`i',1] if relationship==0 & koen=="1" & fined==`i'
}

forvalues i=1(1)4{
	replace weights=educ_w_single_f[1,`i'] if relationship==0 & koen=="2" & fined==`i'
}

replace weights=1 if weights==. /*Then those with missing type are counted same way in factual and counterfactual scenario*/

*Inequality 
forvalues y=2018(1)2018{
qui ineqdeco household_earnings [weight=weights] if aar==`y'

sca gini_cf_marm_educ_`y'=r(gini)
sca p90p50_cf_marm_educ_`y'=r(p90p50)
sca p50p10_cf_marm_educ_`y'=r(p50)/r(p10)
}


frame change results

gen gini_cf_marm_educ=.
forvalues i=2018(1)2018{
replace gini_cf_marm_educ=gini_cf_marm_educ_`i' if aar==`i' 
}

gen p90p50_cf_marm_educ=.
forvalues i=2018(1)2018{
replace p90p50_cf_marm_educ=p90p50_cf_marm_educ_`i' if aar==`i' 
}

gen p50p10_cf_marm_educ=.
forvalues i=2018(1)2018{
replace p50p10_cf_marm_educ=p50p10_cf_marm_educ_`i' if aar==`i' 
}

frame change default



drop weights


*obtain weights - field

*numerator 1
mat field_num_1_couples=J(8,8,.) /*The prob. of counterfactual given couple cell x,x*/

forvalues i=1(1)8{
	forvalues j=1(1)8{
		mat field_num_1_couples[`i',`j']=field_cf_marm_2018[`i',`j']/(field_cf_marm_2018[`i',`j']+field_2018[`i',`j'])
	}
}

mat field_num_1_single_m=J(8,1,.) /*The prob. of counterfactual given single men cell x*/

forvalues i=1(1)8{
	mat field_num_1_single_m[`i',1]=field_single_m_cf_marm_2018[`i',1]/(field_single_m_cf_marm_2018[`i',1]+field_single_m_2018[`i',1])
}

mat field_num_1_single_f=J(1,8,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)8{
	mat field_num_1_single_f[1,`i']=field_single_f_cf_marm_2018[1,`i']/(field_single_f_cf_marm_2018[1,`i']+field_single_f_2018[1,`i'])
}

*denominator 1
mat field_de_1_couples=J(8,8,.) /*The prob. of factual given couple cell x,x*/

forvalues i=1(1)8{
	forvalues j=1(1)8{
		mat field_de_1_couples[`i',`j']=field_2018[`i',`j']/(field_cf_marm_2018[`i',`j']+field_2018[`i',`j'])
	}
}

mat field_de_1_single_m=J(8,1,.) /*The prob. of factual given single men cell x*/

forvalues i=1(1)8{
	mat field_de_1_single_m[`i',1]=field_single_m_2018[`i',1]/(field_single_m_cf_marm_2018[`i',1]+field_single_m_2018[`i',1])
}

mat field_de_1_single_f=J(1,8,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)8{
	mat field_de_1_single_f[1,`i']=field_single_f_2018[1,`i']/(field_single_f_cf_marm_2018[1,`i']+field_single_f_2018[1,`i'])
}

*numerator 2 /*The prob. of being in factual sample*/

sca number_factual=field_2018[1,1]+field_2018[1,2]+field_2018[1,3]+field_2018[1,4]+field_2018[1,5]+field_2018[1,6]+field_2018[1,7]+field_2018[1,8] ///
	+field_2018[2,1]+field_2018[2,2]+field_2018[2,3]+field_2018[2,4]+field_2018[2,5]+field_2018[2,6]+field_2018[2,7]+field_2018[2,8] ///
	+field_2018[3,1]+field_2018[3,2]+field_2018[3,3]+field_2018[3,4]+field_2018[3,5]+field_2018[3,6]+field_2018[3,7]+field_2018[3,8] ///
	+field_2018[4,1]+field_2018[4,2]+field_2018[4,3]+field_2018[4,4]+field_2018[4,5]+field_2018[4,6]+field_2018[4,7]+field_2018[4,8] ///
	+field_2018[5,1]+field_2018[5,2]+field_2018[5,3]+field_2018[5,4]+field_2018[5,5]+field_2018[5,6]+field_2018[5,7]+field_2018[5,8] ///
	+field_2018[6,1]+field_2018[6,2]+field_2018[6,3]+field_2018[6,4]+field_2018[6,5]+field_2018[6,6]+field_2018[6,7]+field_2018[6,8] ///
	+field_2018[7,1]+field_2018[7,2]+field_2018[7,3]+field_2018[7,4]+field_2018[7,5]+field_2018[7,6]+field_2018[7,7]+field_2018[7,8] ///
	+field_2018[8,1]+field_2018[8,2]+field_2018[8,3]+field_2018[8,4]+field_2018[8,5]+field_2018[8,6]+field_2018[8,7]+field_2018[8,8] ///
	+field_single_m_2018[1,1]+field_single_m_2018[2,1]+field_single_m_2018[3,1]+field_single_m_2018[4,1]+field_single_m_2018[5,1]+field_single_m_2018[6,1]+field_single_m_2018[7,1]+field_single_m_2018[8,1] ///
	+field_single_f_2018[1,1]+field_single_f_2018[1,2]+field_single_f_2018[1,3]+field_single_f_2018[1,4]+field_single_f_2018[1,5]+field_single_f_2018[1,6]+field_single_f_2018[1,7]+field_single_f_2018[1,8]
	
sca number_factual=field_cf_marm_2018[1,1]+field_cf_marm_2018[1,2]+field_cf_marm_2018[1,3]+field_cf_marm_2018[1,4]+field_cf_marm_2018[1,5]+field_cf_marm_2018[1,6]+field_cf_marm_2018[1,7]+field_cf_marm_2018[1,8] ///
	+field_cf_marm_2018[2,1]+field_cf_marm_2018[2,2]+field_cf_marm_2018[2,3]+field_cf_marm_2018[2,4]+field_cf_marm_2018[2,5]+field_cf_marm_2018[2,6]+field_cf_marm_2018[2,7]+field_cf_marm_2018[2,8] ///
	+field_cf_marm_2018[3,1]+field_cf_marm_2018[3,2]+field_cf_marm_2018[3,3]+field_cf_marm_2018[3,4]+field_cf_marm_2018[3,5]+field_cf_marm_2018[3,6]+field_cf_marm_2018[3,7]+field_cf_marm_2018[3,8] ///
	+field_cf_marm_2018[4,1]+field_cf_marm_2018[4,2]+field_cf_marm_2018[4,3]+field_cf_marm_2018[4,4]+field_cf_marm_2018[4,5]+field_cf_marm_2018[4,6]+field_cf_marm_2018[4,7]+field_cf_marm_2018[4,8] ///
	+field_cf_marm_2018[5,1]+field_cf_marm_2018[5,2]+field_cf_marm_2018[5,3]+field_cf_marm_2018[5,4]+field_cf_marm_2018[5,5]+field_cf_marm_2018[5,6]+field_cf_marm_2018[5,7]+field_cf_marm_2018[5,8] ///
	+field_cf_marm_2018[6,1]+field_cf_marm_2018[6,2]+field_cf_marm_2018[6,3]+field_cf_marm_2018[6,4]+field_cf_marm_2018[6,5]+field_cf_marm_2018[6,6]+field_cf_marm_2018[6,7]+field_cf_marm_2018[6,8] ///
	+field_cf_marm_2018[7,1]+field_cf_marm_2018[7,2]+field_cf_marm_2018[7,3]+field_cf_marm_2018[7,4]+field_cf_marm_2018[7,5]+field_cf_marm_2018[7,6]+field_cf_marm_2018[7,7]+field_cf_marm_2018[7,8] ///
	+field_cf_marm_2018[8,1]+field_cf_marm_2018[8,2]+field_cf_marm_2018[8,3]+field_cf_marm_2018[8,4]+field_cf_marm_2018[8,5]+field_cf_marm_2018[8,6]+field_cf_marm_2018[8,7]+field_cf_marm_2018[8,8] ///
	+field_single_m_cf_marm_2018[1,1]+field_single_m_cf_marm_2018[2,1]+field_single_m_cf_marm_2018[3,1]+field_single_m_cf_marm_2018[4,1]+field_single_m_cf_marm_2018[5,1]+field_single_m_cf_marm_2018[6,1]+field_single_m_cf_marm_2018[7,1]+field_single_m_cf_marm_2018[8,1] ///
	+field_single_f_cf_marm_2018[1,1]+field_single_f_cf_marm_2018[1,2]+field_single_f_cf_marm_2018[1,3]+field_single_f_cf_marm_2018[1,4]+field_single_f_cf_marm_2018[1,5]+field_single_f_cf_marm_2018[1,6]+field_single_f_cf_marm_2018[1,7]+field_single_f_cf_marm_2018[1,8]

	
sca field_num_2=number_factual/(number_factual+number_counterfactual)

*denominator 2 /*The prob. of being in counterfactual sample*/
sca field_de_2=number_counterfactual/(number_factual+number_counterfactual)


*weights
mat field_w_couples=J(8,8,.)

forvalues i=1(1)8{
	forvalues j=1(1)8{
		mat field_w_couples[`i',`j']=(field_num_1_couples[`i',`j']/field_de_1_couples[`i',`j'])*(field_num_2/field_de_2)
	}
}

mat field_w_single_m=J(8,1,.)

forvalues i=1(1)8{
		mat field_w_single_m[`i',1]=(field_num_1_single_m[`i',1]/field_de_1_single_m[`i',1])*(field_num_2/field_de_2)
}

mat field_w_single_f=J(1,8,.)

forvalues i=1(1)8{
		mat field_w_single_f[1,`i']=(field_num_1_single_f[1,`i']/field_de_1_single_f[1,`i'])*(field_num_2/field_de_2)
}

gen weights=.
forvalues i=1(1)8{
	forvalues j=1(1)8{
replace weights=field_w_couples[`i',`j'] if field_male==`i' & field_female==`j' & relationship==1
	}
}

forvalues i=1(1)8{
	replace weights=field_w_single_m[`i',1] if relationship==0 & koen=="1" & educ_level_field_num==`i'
}

forvalues i=1(1)8{
	replace weights=field_w_single_f[1,`i'] if relationship==0 & koen=="2" & educ_level_field_num==`i'
}

replace weights=1 if weights==. /*Then those with missing type are counted same way in factual and counterfactual scenario*/

*Inequality 
forvalues y=2018(1)2018{
qui ineqdeco household_earnings [weight=weights] if aar==`y'

sca gini_cf_marm_field_`y'=r(gini)
sca p90p50_cf_marm_field_`y'=r(p90p50)
sca p50p10_cf_marm_field_`y'=r(p50)/r(p10)
}


frame change results

gen gini_cf_marm_field=.
forvalues i=2018(1)2018{
replace gini_cf_marm_field=gini_cf_marm_field_`i' if aar==`i' 
}

gen p90p50_cf_marm_field=.
forvalues i=2018(1)2018{
replace p90p50_cf_marm_field=p90p50_cf_marm_field_`i' if aar==`i' 
}

gen p50p10_cf_marm_field=.
forvalues i=2018(1)2018{
replace p50p10_cf_marm_field=p50p10_cf_marm_field_`i' if aar==`i' 
}

frame change default



drop weights

}


*Fixed marginals - female only*
{
*Solve for 2018 single numbers with 2018 preferences and 1980 marginals (female)*

*ambition types

program nlfaq

syntax varlist(min=1 max=1) [if], at(name)

tempname m1 m2 m3 m4 f1 f2 f3 f4

scalar `m1' = `at'[1,1]
scalar `m2' = `at'[1,2]
scalar `m3' = `at'[1,3]
scalar `m4' = `at'[1,4]
scalar `f1' = `at'[1,5]
scalar `f2' = `at'[1,6]
scalar `f3' = `at'[1,7]
scalar `f4' = `at'[1,8]

tempvar yh
gen double `yh'= am_pi_2018[1,1]*((`m1'*`f1')^(1/2))*((am_mar_m_2018[1,1]*am_mar_f_base[1,1])^((1/2)))+am_pi_2018[1,2]*((`m1'*`f2')^(1/2))*(((am_mar_m_2018[1,1]*am_mar_f_base[1,2]))^((1/2)))+am_pi_2018[1,3]*((`m1'*`f3')^(1/2))*(((am_mar_m_2018[1,1]*am_mar_f_base[1,3]))^((1/2)))+am_pi_2018[1,4]*((`m1'*`f4')^(1/2))*(((am_mar_m_2018[1,1]*am_mar_f_base[1,4]))^((1/2)))+`m1'-am_mar_m_2018[1,1]+1 in 1

replace `yh'= am_pi_2018[2,1]*((`m2'*`f1')^(1/2))*(((am_mar_m_2018[2,1]*am_mar_f_base[1,1]))^((1/2)))+am_pi_2018[2,2]*((`m2'*`f2')^(1/2))*(((am_mar_m_2018[2,1]*am_mar_f_base[1,2]))^((1/2)))+am_pi_2018[2,3]*((`m2'*`f3')^(1/2))*(((am_mar_m_2018[2,1]*am_mar_f_base[1,3]))^((1/2)))+am_pi_2018[2,4]*((`m2'*`f4')^(1/2))*(((am_mar_m_2018[2,1]*am_mar_f_base[1,4]))^((1/2)))+`m2'-am_mar_m_2018[2,1]+1 in 2

replace `yh'= am_pi_2018[3,1]*((`m3'*`f1')^(1/2))*(((am_mar_m_2018[3,1]*am_mar_f_base[1,1]))^((1/2)))+am_pi_2018[3,2]*((`m3'*`f2')^(1/2))*(((am_mar_m_2018[3,1]*am_mar_f_base[1,2]))^((1/2)))+am_pi_2018[3,3]*((`m3'*`f3')^(1/2))*(((am_mar_m_2018[3,1]*am_mar_f_base[1,3]))^((1/2)))+am_pi_2018[3,4]*((`m3'*`f4')^(1/2))*(((am_mar_m_2018[3,1]*am_mar_f_base[1,4]))^((1/2)))+`m3'-am_mar_m_2018[3,1]+1 in 3

replace `yh'= am_pi_2018[4,1]*((`m4'*`f1')^(1/2))*(((am_mar_m_2018[4,1]*am_mar_f_base[1,1]))^((1/2)))+am_pi_2018[4,2]*((`m4'*`f2')^(1/2))*(((am_mar_m_2018[4,1]*am_mar_f_base[1,2]))^((1/2)))+am_pi_2018[4,3]*((`m4'*`f3')^(1/2))*(((am_mar_m_2018[4,1]*am_mar_f_base[1,3]))^((1/2)))+am_pi_2018[4,4]*((`m4'*`f4')^(1/2))*(((am_mar_m_2018[4,1]*am_mar_f_base[1,4]))^((1/2)))+`m4'-am_mar_m_2018[4,1]+1 in 4

replace `yh'=am_pi_2018[1,1]*((`m1'*`f1')^(1/2))*(((am_mar_m_2018[1,1]*am_mar_f_base[1,1]))^((1/2)))+am_pi_2018[2,1]*((`m2'*`f1')^(1/2))*(((am_mar_m_2018[2,1]*am_mar_f_base[1,1]))^((1/2)))+am_pi_2018[3,1]*((`m3'*`f1')^(1/2))*(((am_mar_m_2018[3,1]*am_mar_f_base[1,1]))^((1/2)))+am_pi_2018[4,1]*((`m4'*`f1')^(1/2))*(((am_mar_m_2018[4,1]*am_mar_f_base[1,1]))^((1/2)))+`f1'-am_mar_f_base[1,1] in 5

replace `yh'=am_pi_2018[1,2]*((`m1'*`f2')^(1/2))*(((am_mar_m_2018[1,1]*am_mar_f_base[1,2]))^((1/2)))+am_pi_2018[2,2]*((`m2'*`f2')^(1/2))*(((am_mar_m_2018[2,1]*am_mar_f_base[1,2]))^((1/2)))+am_pi_2018[3,2]*((`m3'*`f2')^(1/2))*(((am_mar_m_2018[3,1]*am_mar_f_base[1,2]))^((1/2)))+am_pi_2018[4,2]*((`m4'*`f2')^(1/2))*(((am_mar_m_2018[4,1]*am_mar_f_base[1,2]))^((1/2)))+`f2'-am_mar_f_base[1,2] in 6

replace `yh'=am_pi_2018[1,3]*((`m1'*`f3')^(1/2))*(((am_mar_m_2018[1,1]*am_mar_f_base[1,3]))^((1/2)))+am_pi_2018[2,3]*((`m2'*`f3')^(1/2))*(((am_mar_m_2018[2,1]*am_mar_f_base[1,3]))^((1/2)))+am_pi_2018[3,3]*((`m3'*`f3')^(1/2))*(((am_mar_m_2018[3,1]*am_mar_f_base[1,3]))^((1/2)))+am_pi_2018[4,3]*((`m4'*`f3')^(1/2))*(((am_mar_m_2018[4,1]*am_mar_f_base[1,3]))^((1/2)))+`f3'-am_mar_f_base[1,3] in 7

replace `yh'=am_pi_2018[1,4]*((`m1'*`f4')^(1/2))*(((am_mar_m_2018[1,1]*am_mar_f_base[1,4]))^((1/2)))+am_pi_2018[2,4]*((`m2'*`f4')^(1/2))*(((am_mar_m_2018[2,1]*am_mar_f_base[1,4]))^((1/2)))+am_pi_2018[3,4]*((`m3'*`f4')^(1/2))*(((am_mar_m_2018[3,1]*am_mar_f_base[1,4]))^((1/2)))+am_pi_2018[4,4]*((`m4'*`f4')^(1/2))*(((am_mar_m_2018[4,1]*am_mar_f_base[1,4]))^((1/2)))+`f4'-am_mar_f_base[1,4] in 8



replace `varlist'=`yh'
end


frame create temp
frame change temp

clear
set obs 8

gen y = 0
replace y=1 in 1

nl faq @ y, parameters(m1 m2 m3 m4 f1 f2 f3 f4) initial(m1 1 m2 1 m3 1 m4 1 f1 1 f2 1 f3 1 f4 1)

*get counter factual singles
mat am_single_m_cf_marf_2018=J(4,1,.)
mat am_single_m_cf_marf_2018[1,1]=[m1]_b[_cons]
mat am_single_m_cf_marf_2018[2,1]=[m2]_b[_cons]
mat am_single_m_cf_marf_2018[3,1]=[m3]_b[_cons]
mat am_single_m_cf_marf_2018[4,1]=[m4]_b[_cons]

mat am_single_f_cf_marf_2018=J(1,4,.)
mat am_single_f_cf_marf_2018[1,1]=[f1]_b[_cons]
mat am_single_f_cf_marf_2018[1,2]=[f2]_b[_cons]
mat am_single_f_cf_marf_2018[1,3]=[f3]_b[_cons]
mat am_single_f_cf_marf_2018[1,4]=[f4]_b[_cons]


frame change default
frame drop temp
prog drop nlfaq




*education types
program nlfaq

syntax varlist(min=1 max=1) [if], at(name)

tempname m1 m2 m3 m4 f1 f2 f3 f4

scalar `m1' = `at'[1,1]
scalar `m2' = `at'[1,2]
scalar `m3' = `at'[1,3]
scalar `m4' = `at'[1,4]
scalar `f1' = `at'[1,5]
scalar `f2' = `at'[1,6]
scalar `f3' = `at'[1,7]
scalar `f4' = `at'[1,8]

tempvar yh
gen double `yh'= educ_pi_2018[1,1]*((`m1'*`f1')^(1/2))*((educ_mar_m_2018[1,1]*educ_mar_f_base[1,1])^((1/2)))+educ_pi_2018[1,2]*((`m1'*`f2')^(1/2))*(((educ_mar_m_2018[1,1]*educ_mar_f_base[1,2]))^((1/2)))+educ_pi_2018[1,3]*((`m1'*`f3')^(1/2))*(((educ_mar_m_2018[1,1]*educ_mar_f_base[1,3]))^((1/2)))+educ_pi_2018[1,4]*((`m1'*`f4')^(1/2))*(((educ_mar_m_2018[1,1]*educ_mar_f_base[1,4]))^((1/2)))+`m1'-educ_mar_m_2018[1,1]+1 in 1

replace `yh'= educ_pi_2018[2,1]*((`m2'*`f1')^(1/2))*(((educ_mar_m_2018[2,1]*educ_mar_f_base[1,1]))^((1/2)))+educ_pi_2018[2,2]*((`m2'*`f2')^(1/2))*(((educ_mar_m_2018[2,1]*educ_mar_f_base[1,2]))^((1/2)))+educ_pi_2018[2,3]*((`m2'*`f3')^(1/2))*(((educ_mar_m_2018[2,1]*educ_mar_f_base[1,3]))^((1/2)))+educ_pi_2018[2,4]*((`m2'*`f4')^(1/2))*(((educ_mar_m_2018[2,1]*educ_mar_f_base[1,4]))^((1/2)))+`m2'-educ_mar_m_2018[2,1]+1 in 2

replace `yh'= educ_pi_2018[3,1]*((`m3'*`f1')^(1/2))*(((educ_mar_m_2018[3,1]*educ_mar_f_base[1,1]))^((1/2)))+educ_pi_2018[3,2]*((`m3'*`f2')^(1/2))*(((educ_mar_m_2018[3,1]*educ_mar_f_base[1,2]))^((1/2)))+educ_pi_2018[3,3]*((`m3'*`f3')^(1/2))*(((educ_mar_m_2018[3,1]*educ_mar_f_base[1,3]))^((1/2)))+educ_pi_2018[3,4]*((`m3'*`f4')^(1/2))*(((educ_mar_m_2018[3,1]*educ_mar_f_base[1,4]))^((1/2)))+`m3'-educ_mar_m_2018[3,1]+1 in 3

replace `yh'= educ_pi_2018[4,1]*((`m4'*`f1')^(1/2))*(((educ_mar_m_2018[4,1]*educ_mar_f_base[1,1]))^((1/2)))+educ_pi_2018[4,2]*((`m4'*`f2')^(1/2))*(((educ_mar_m_2018[4,1]*educ_mar_f_base[1,2]))^((1/2)))+educ_pi_2018[4,3]*((`m4'*`f3')^(1/2))*(((educ_mar_m_2018[4,1]*educ_mar_f_base[1,3]))^((1/2)))+educ_pi_2018[4,4]*((`m4'*`f4')^(1/2))*(((educ_mar_m_2018[4,1]*educ_mar_f_base[1,4]))^((1/2)))+`m4'-educ_mar_m_2018[4,1]+1 in 4

replace `yh'=educ_pi_2018[1,1]*((`m1'*`f1')^(1/2))*(((educ_mar_m_2018[1,1]*educ_mar_f_base[1,1]))^((1/2)))+educ_pi_2018[2,1]*((`m2'*`f1')^(1/2))*(((educ_mar_m_2018[2,1]*educ_mar_f_base[1,1]))^((1/2)))+educ_pi_2018[3,1]*((`m3'*`f1')^(1/2))*(((educ_mar_m_2018[3,1]*educ_mar_f_base[1,1]))^((1/2)))+educ_pi_2018[4,1]*((`m4'*`f1')^(1/2))*(((educ_mar_m_2018[4,1]*educ_mar_f_base[1,1]))^((1/2)))+`f1'-educ_mar_f_base[1,1] in 5

replace `yh'=educ_pi_2018[1,2]*((`m1'*`f2')^(1/2))*(((educ_mar_m_2018[1,1]*educ_mar_f_base[1,2]))^((1/2)))+educ_pi_2018[2,2]*((`m2'*`f2')^(1/2))*(((educ_mar_m_2018[2,1]*educ_mar_f_base[1,2]))^((1/2)))+educ_pi_2018[3,2]*((`m3'*`f2')^(1/2))*(((educ_mar_m_2018[3,1]*educ_mar_f_base[1,2]))^((1/2)))+educ_pi_2018[4,2]*((`m4'*`f2')^(1/2))*(((educ_mar_m_2018[4,1]*educ_mar_f_base[1,2]))^((1/2)))+`f2'-educ_mar_f_base[1,2] in 6

replace `yh'=educ_pi_2018[1,3]*((`m1'*`f3')^(1/2))*(((educ_mar_m_2018[1,1]*educ_mar_f_base[1,3]))^((1/2)))+educ_pi_2018[2,3]*((`m2'*`f3')^(1/2))*(((educ_mar_m_2018[2,1]*educ_mar_f_base[1,3]))^((1/2)))+educ_pi_2018[3,3]*((`m3'*`f3')^(1/2))*(((educ_mar_m_2018[3,1]*educ_mar_f_base[1,3]))^((1/2)))+educ_pi_2018[4,3]*((`m4'*`f3')^(1/2))*(((educ_mar_m_2018[4,1]*educ_mar_f_base[1,3]))^((1/2)))+`f3'-educ_mar_f_base[1,3] in 7

replace `yh'=educ_pi_2018[1,4]*((`m1'*`f4')^(1/2))*(((educ_mar_m_2018[1,1]*educ_mar_f_base[1,4]))^((1/2)))+educ_pi_2018[2,4]*((`m2'*`f4')^(1/2))*(((educ_mar_m_2018[2,1]*educ_mar_f_base[1,4]))^((1/2)))+educ_pi_2018[3,4]*((`m3'*`f4')^(1/2))*(((educ_mar_m_2018[3,1]*educ_mar_f_base[1,4]))^((1/2)))+educ_pi_2018[4,4]*((`m4'*`f4')^(1/2))*(((educ_mar_m_2018[4,1]*educ_mar_f_base[1,4]))^((1/2)))+`f4'-educ_mar_f_base[1,4] in 8


replace `varlist'=`yh'
end


frame create temp
frame change temp

clear
set obs 8

gen y = 0
replace y=1 in 1

nl faq @ y, parameters(m1 m2 m3 m4 f1 f2 f3 f4) initial(m1 1 m2 1 m3 1 m4 1 f1 1 f2 1 f3 1 f4 1)

*get counter factual singles
mat educ_single_m_cf_marf_2018=J(4,1,.)
mat educ_single_m_cf_marf_2018[1,1]=[m1]_b[_cons]
mat educ_single_m_cf_marf_2018[2,1]=[m2]_b[_cons]
mat educ_single_m_cf_marf_2018[3,1]=[m3]_b[_cons]
mat educ_single_m_cf_marf_2018[4,1]=[m4]_b[_cons]

mat educ_single_f_cf_marf_2018=J(1,4,.)
mat educ_single_f_cf_marf_2018[1,1]=[f1]_b[_cons]
mat educ_single_f_cf_marf_2018[1,2]=[f2]_b[_cons]
mat educ_single_f_cf_marf_2018[1,3]=[f3]_b[_cons]
mat educ_single_f_cf_marf_2018[1,4]=[f4]_b[_cons]


frame change default
frame drop temp
prog drop nlfaq

*field types
program nlfaq

syntax varlist(min=1 max=1) [if], at(name)

tempname m1 m2 m3 m4 m5 m6 m7 m8 f1 f2 f3 f4 f5 f6 f7 f8

scalar `m1' = `at'[1,1]
scalar `m2' = `at'[1,2]
scalar `m3' = `at'[1,3]
scalar `m4' = `at'[1,4]
scalar `m5' = `at'[1,5]
scalar `m6' = `at'[1,6]
scalar `m7' = `at'[1,7]
scalar `m8' = `at'[1,8]
scalar `f1' = `at'[1,9]
scalar `f2' = `at'[1,10]
scalar `f3' = `at'[1,11]
scalar `f4' = `at'[1,12]
scalar `f5' = `at'[1,13]
scalar `f6' = `at'[1,14]
scalar `f7' = `at'[1,15]
scalar `f8' = `at'[1,16]


tempvar yh
gen double `yh'= field_pi_2018[1,1]*((`m1'*`f1')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[1,2]*((`m1'*`f2')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[1,3]*((`m1'*`f3')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[1,4]*((`m1'*`f4')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[1,5]*((`m1'*`f5')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[1,6]*((`m1'*`f6')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[1,7]*((`m1'*`f7')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[1,8]*((`m1'*`f8')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_base[1,8]))^((1/2)))+`m1'-field_mar_m_2018[1,1]+1 in 1

replace `yh'= field_pi_2018[2,1]*((`m2'*`f1')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[2,2]*((`m2'*`f2')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[2,3]*((`m2'*`f3')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[2,4]*((`m2'*`f4')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[2,5]*((`m2'*`f5')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[2,6]*((`m2'*`f6')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[2,7]*((`m2'*`f7')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[2,8]*((`m2'*`f8')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_base[1,8]))^((1/2)))+`m2'-field_mar_m_2018[2,1]+1 in 2

replace `yh'= field_pi_2018[3,1]*((`m3'*`f1')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[3,2]*((`m3'*`f2')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[3,3]*((`m3'*`f3')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[3,4]*((`m3'*`f4')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[3,5]*((`m3'*`f5')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[3,6]*((`m3'*`f6')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[3,7]*((`m3'*`f7')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[3,8]*((`m3'*`f8')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_base[1,8]))^((1/2)))+`m3'-field_mar_m_2018[3,1]+1 in 3

replace `yh'= field_pi_2018[4,1]*((`m4'*`f1')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[4,2]*((`m4'*`f2')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[4,3]*((`m4'*`f3')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[4,4]*((`m4'*`f4')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[4,5]*((`m4'*`f5')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[4,6]*((`m4'*`f6')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[4,7]*((`m4'*`f7')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[4,8]*((`m4'*`f8')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_base[1,8]))^((1/2)))+`m4'-field_mar_m_2018[4,1]+1 in 4

replace `yh'= field_pi_2018[5,1]*((`m5'*`f1')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[5,2]*((`m5'*`f2')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[5,3]*((`m5'*`f3')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[5,4]*((`m5'*`f4')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[5,5]*((`m5'*`f5')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[5,6]*((`m5'*`f6')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[5,7]*((`m5'*`f7')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[5,8]*((`m5'*`f8')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_base[1,8]))^((1/2)))+`m5'-field_mar_m_2018[5,1]+1 in 5

replace `yh'= field_pi_2018[6,1]*((`m6'*`f1')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[6,2]*((`m6'*`f2')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[6,3]*((`m6'*`f3')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[6,4]*((`m6'*`f4')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[6,5]*((`m6'*`f5')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[6,6]*((`m6'*`f6')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[6,7]*((`m6'*`f7')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[6,8]*((`m6'*`f8')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_base[1,8]))^((1/2)))+`m6'-field_mar_m_2018[6,1]+1 in 6

replace `yh'= field_pi_2018[7,1]*((`m7'*`f1')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[7,2]*((`m7'*`f2')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[7,3]*((`m7'*`f3')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[7,4]*((`m7'*`f4')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[7,5]*((`m7'*`f5')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[7,6]*((`m7'*`f6')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[7,7]*((`m7'*`f7')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[7,8]*((`m7'*`f8')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_base[1,8]))^((1/2)))+`m7'-field_mar_m_2018[7,1]+1 in 7

replace `yh'= field_pi_2018[8,1]*((`m8'*`f1')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[8,2]*((`m8'*`f2')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[8,3]*((`m8'*`f3')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[8,4]*((`m8'*`f4')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[8,5]*((`m8'*`f5')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[8,6]*((`m8'*`f6')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[8,7]*((`m8'*`f7')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[8,8]*((`m8'*`f8')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_base[1,8]))^((1/2)))+`m8'-field_mar_m_2018[8,1]+1 in 8

replace `yh'=field_pi_2018[1,1]*((`m1'*`f1')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[2,1]*((`m2'*`f1')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[3,1]*((`m3'*`f1')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[4,1]*((`m4'*`f1')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[5,1]*((`m5'*`f1')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[6,1]*((`m6'*`f1')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[7,1]*((`m7'*`f1')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_base[1,1]))^((1/2)))+field_pi_2018[8,1]*((`m8'*`f1')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_base[1,1]))^((1/2)))+`f1'-field_mar_f_base[1,1] in 9

replace `yh'=field_pi_2018[1,2]*((`m1'*`f2')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[2,2]*((`m2'*`f2')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[3,2]*((`m3'*`f2')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[4,2]*((`m4'*`f2')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[5,2]*((`m5'*`f2')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[6,2]*((`m6'*`f2')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[7,2]*((`m7'*`f2')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_base[1,2]))^((1/2)))+field_pi_2018[8,2]*((`m8'*`f2')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_base[1,2]))^((1/2)))+`f2'-field_mar_f_base[1,2] in 10

replace `yh'=field_pi_2018[1,3]*((`m1'*`f3')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[2,3]*((`m2'*`f3')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[3,3]*((`m3'*`f3')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[4,3]*((`m4'*`f3')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[5,3]*((`m5'*`f3')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[6,3]*((`m6'*`f3')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[7,3]*((`m7'*`f3')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_base[1,3]))^((1/2)))+field_pi_2018[8,3]*((`m8'*`f3')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_base[1,3]))^((1/2)))+`f3'-field_mar_f_base[1,3] in 11

replace `yh'=field_pi_2018[1,4]*((`m1'*`f4')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[2,4]*((`m2'*`f4')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[3,4]*((`m3'*`f4')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[4,4]*((`m4'*`f4')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[5,4]*((`m5'*`f4')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[6,4]*((`m6'*`f4')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[7,4]*((`m7'*`f4')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_base[1,4]))^((1/2)))+field_pi_2018[8,4]*((`m8'*`f4')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_base[1,4]))^((1/2)))+`f4'-field_mar_f_base[1,4] in 12

replace `yh'=field_pi_2018[1,5]*((`m1'*`f5')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[2,5]*((`m2'*`f5')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[3,5]*((`m3'*`f5')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[4,5]*((`m4'*`f5')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[5,5]*((`m5'*`f5')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[6,5]*((`m6'*`f5')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[7,5]*((`m7'*`f5')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_base[1,5]))^((1/2)))+field_pi_2018[8,5]*((`m8'*`f5')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_base[1,5]))^((1/2)))+`f5'-field_mar_f_base[1,5] in 13

replace `yh'=field_pi_2018[1,6]*((`m1'*`f6')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[2,6]*((`m2'*`f6')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[3,6]*((`m3'*`f6')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[4,6]*((`m4'*`f6')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[5,6]*((`m5'*`f6')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[6,6]*((`m6'*`f6')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[7,6]*((`m7'*`f6')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_base[1,6]))^((1/2)))+field_pi_2018[8,6]*((`m8'*`f6')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_base[1,6]))^((1/2)))+`f6'-field_mar_f_base[1,6] in 14

replace `yh'=field_pi_2018[1,7]*((`m1'*`f7')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[2,7]*((`m2'*`f7')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[3,7]*((`m3'*`f7')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[4,7]*((`m4'*`f7')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[5,7]*((`m5'*`f7')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[6,7]*((`m6'*`f7')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[7,7]*((`m7'*`f7')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_base[1,7]))^((1/2)))+field_pi_2018[8,7]*((`m8'*`f7')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_base[1,7]))^((1/2)))+`f7'-field_mar_f_base[1,7] in 15

replace `yh'=field_pi_2018[1,8]*((`m1'*`f8')^(1/2))*(((field_mar_m_2018[1,1]*field_mar_f_base[1,8]))^((1/2)))+field_pi_2018[2,8]*((`m2'*`f8')^(1/2))*(((field_mar_m_2018[2,1]*field_mar_f_base[1,8]))^((1/2)))+field_pi_2018[3,8]*((`m3'*`f8')^(1/2))*(((field_mar_m_2018[3,1]*field_mar_f_base[1,8]))^((1/2)))+field_pi_2018[4,8]*((`m4'*`f8')^(1/2))*(((field_mar_m_2018[4,1]*field_mar_f_base[1,8]))^((1/2)))+field_pi_2018[5,8]*((`m5'*`f8')^(1/2))*(((field_mar_m_2018[5,1]*field_mar_f_base[1,8]))^((1/2)))+field_pi_2018[6,8]*((`m6'*`f8')^(1/2))*(((field_mar_m_2018[6,1]*field_mar_f_base[1,8]))^((1/2)))+field_pi_2018[7,8]*((`m7'*`f8')^(1/2))*(((field_mar_m_2018[7,1]*field_mar_f_base[1,8]))^((1/2)))+field_pi_2018[8,8]*((`m8'*`f8')^(1/2))*(((field_mar_m_2018[8,1]*field_mar_f_base[1,8]))^((1/2)))+`f8'-field_mar_f_base[1,8] in 16




replace `varlist'=`yh'
end


frame create temp
frame change temp

clear
set obs 16

gen y = 0
replace y=1 in 1

nl faq @ y, parameters(m1 m2 m3 m4 m5 m6 m7 m8 f1 f2 f3 f4 f5 f6 f7 f8) initial(m1 1 m2 1 m3 1 m4 1 m5 1 m6 1 m7 1 m8 1 f1 1 f2 1 f3 1 f4 1 f5 1 f6 1 f7 1 f8 1)

*get counter factual singles
mat field_single_m_cf_marf_2018=J(8,1,.)
mat field_single_m_cf_marf_2018[1,1]=[m1]_b[_cons]
mat field_single_m_cf_marf_2018[2,1]=[m2]_b[_cons]
mat field_single_m_cf_marf_2018[3,1]=[m3]_b[_cons]
mat field_single_m_cf_marf_2018[4,1]=[m4]_b[_cons]
mat field_single_m_cf_marf_2018[5,1]=[m5]_b[_cons]
mat field_single_m_cf_marf_2018[6,1]=[m6]_b[_cons]
mat field_single_m_cf_marf_2018[7,1]=[m7]_b[_cons]
mat field_single_m_cf_marf_2018[8,1]=[m8]_b[_cons]

mat field_single_f_cf_marf_2018=J(1,8,.)
mat field_single_f_cf_marf_2018[1,1]=[f1]_b[_cons]
mat field_single_f_cf_marf_2018[1,2]=[f2]_b[_cons]
mat field_single_f_cf_marf_2018[1,3]=[f3]_b[_cons]
mat field_single_f_cf_marf_2018[1,4]=[f4]_b[_cons]
mat field_single_f_cf_marf_2018[1,5]=[f5]_b[_cons]
mat field_single_f_cf_marf_2018[1,6]=[f6]_b[_cons]
mat field_single_f_cf_marf_2018[1,7]=[f7]_b[_cons]
mat field_single_f_cf_marf_2018[1,8]=[f8]_b[_cons]


frame change default
frame drop temp
prog drop nlfaq

*get counter factual couple numbers*

mat am_cf_marf_2018=J(4,4,.)

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat am_cf_marf_2018[`i',`j']=am_pi_2018[`i',`j']*((am_single_m_cf_marf_2018[`i',1]*am_single_f_cf_marf_2018[1,`j'])^(1/2))*((am_mar_m_2018[`i',1]*am_mar_f_base[1,`j'])^((1/2)))
	}
}

mat educ_cf_marf_2018=J(4,4,.)

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat educ_cf_marf_2018[`i',`j']=educ_pi_2018[`i',`j']*((educ_single_m_cf_marf_2018[`i',1]*educ_single_f_cf_marf_2018[1,`j'])^(1/2))*((educ_mar_m_2018[`i',1]*educ_mar_f_base[1,`j'])^((1/2)))
	}
}

mat field_cf_marf_2018=J(8,8,.)

forvalues i=1(1)8{
	forvalues j=1(1)8{
		mat field_cf_marf_2018[`i',`j']=field_pi_2018[`i',`j']*((field_single_m_cf_marf_2018[`i',1]*field_single_f_cf_marf_2018[1,`j'])^(1/2))*((field_mar_m_2018[`i',1]*field_mar_f_base[1,`j'])^((1/2)))
	}
}


*obtain weights - ambition

*numerator 1
mat am_num_1_couples=J(4,4,.) /*The prob. of counterfactual given couple cell x,x*/

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat am_num_1_couples[`i',`j']=am_cf_marf_2018[`i',`j']/(am_cf_marf_2018[`i',`j']+am_2018[`i',`j'])
	}
}

mat am_num_1_single_m=J(4,1,.) /*The prob. of counterfactual given single men cell x*/

forvalues i=1(1)4{
	mat am_num_1_single_m[`i',1]=am_single_m_cf_marf_2018[`i',1]/(am_single_m_cf_marf_2018[`i',1]+am_single_m_2018[`i',1])
}

mat am_num_1_single_f=J(1,4,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)4{
	mat am_num_1_single_f[1,`i']=am_single_f_cf_marf_2018[1,`i']/(am_single_f_cf_marf_2018[1,`i']+am_single_f_2018[1,`i'])
}

*denominator 1
mat am_de_1_couples=J(4,4,.) /*The prob. of factual given couple cell x,x*/

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat am_de_1_couples[`i',`j']=am_2018[`i',`j']/(am_cf_marf_2018[`i',`j']+am_2018[`i',`j'])
	}
}

mat am_de_1_single_m=J(4,1,.) /*The prob. of factual given single men cell x*/

forvalues i=1(1)4{
	mat am_de_1_single_m[`i',1]=am_single_m_2018[`i',1]/(am_single_m_cf_marf_2018[`i',1]+am_single_m_2018[`i',1])
}

mat am_de_1_single_f=J(1,4,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)4{
	mat am_de_1_single_f[1,`i']=am_single_f_2018[1,`i']/(am_single_f_cf_marf_2018[1,`i']+am_single_f_2018[1,`i'])
}

*numerator 2 /*The prob. of being in factual sample*/

sca number_factual=am_2018[1,1]+am_2018[1,2]+am_2018[1,3]+am_2018[1,4] ///
	+am_2018[2,1]+am_2018[2,2]+am_2018[2,3]+am_2018[2,4] ///
	+am_2018[3,1]+am_2018[3,2]+am_2018[3,3]+am_2018[3,4] ///
	+am_2018[4,1]+am_2018[4,2]+am_2018[4,3]+am_2018[4,4] ///
	+am_single_m_2018[1,1]+am_single_m_2018[2,1]+am_single_m_2018[3,1]+am_single_m_2018[4,1] ///
	+am_single_f_2018[1,1]+am_single_f_2018[1,2]+am_single_f_2018[1,3]+am_single_f_2018[1,4]
	
sca number_counterfactual=am_cf_marf_2018[1,1]+am_cf_marf_2018[1,2]+am_cf_marf_2018[1,3]+am_cf_marf_2018[1,4] ///
	+am_cf_marf_2018[2,1]+am_cf_marf_2018[2,2]+am_cf_marf_2018[2,3]+am_cf_marf_2018[2,4] ///
	+am_cf_marf_2018[3,1]+am_cf_marf_2018[3,2]+am_cf_marf_2018[3,3]+am_cf_marf_2018[3,4] ///
	+am_cf_marf_2018[4,1]+am_cf_marf_2018[4,2]+am_cf_marf_2018[4,3]+am_cf_marf_2018[4,4] ///
	+am_single_m_cf_marf_2018[1,1]+am_single_m_cf_marf_2018[2,1]+am_single_m_cf_marf_2018[3,1]+am_single_m_cf_marf_2018[4,1] ///
	+am_single_f_cf_marf_2018[1,1]+am_single_f_cf_marf_2018[1,2]+am_single_f_cf_marf_2018[1,3]+am_single_f_cf_marf_2018[1,4]
	
sca am_num_2=number_factual/(number_factual+number_counterfactual)

*denominator 2 /*The prob. of being in counterfactual sample*/
sca am_de_2=number_counterfactual/(number_factual+number_counterfactual)


*weights
mat am_w_couples=J(4,4,.)

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat am_w_couples[`i',`j']=(am_num_1_couples[`i',`j']/am_de_1_couples[`i',`j'])*(am_num_2/am_de_2)
	}
}

mat am_w_single_m=J(4,1,.)

forvalues i=1(1)4{
		mat am_w_single_m[`i',1]=(am_num_1_single_m[`i',1]/am_de_1_single_m[`i',1])*(am_num_2/am_de_2)
}

mat am_w_single_f=J(1,4,.)

forvalues i=1(1)4{
		mat am_w_single_f[1,`i']=(am_num_1_single_f[1,`i']/am_de_1_single_f[1,`i'])*(am_num_2/am_de_2)
}

gen weights=.
forvalues i=1(1)4{
	forvalues j=1(1)4{
replace weights=am_w_couples[`i',`j'] if am_male==`i' & am_female==`j' & relationship==1
	}
}

forvalues i=1(1)4{
	replace weights=am_w_single_m[`i',1] if relationship==0 & koen=="1" & ambition_type_k_4_s==`i'
}

forvalues i=1(1)4{
	replace weights=am_w_single_f[1,`i'] if relationship==0 & koen=="2" & ambition_type_k_4_s==`i'
}

replace weights=1 if weights==. /*Then those with missing type are counted same way in factual and counterfactual scenario*/

*Inequality 
forvalues y=2018(1)2018{
qui ineqdeco household_earnings [weight=weights] if aar==`y'

sca gini_cf_marf_`y'=r(gini)
sca p90p50_cf_marf_`y'=r(p90p50)
sca p50p10_cf_marf_`y'=r(p50)/r(p10)
}


frame change results

gen gini_cf_marf=.
forvalues i=2018(1)2018{
replace gini_cf_marf=gini_cf_marf_`i' if aar==`i' 
}

gen p90p50_cf_marf=.
forvalues i=2018(1)2018{
replace p90p50_cf_marf=p90p50_cf_marf_`i' if aar==`i' 
}

gen p50p10_cf_marf=.
forvalues i=2018(1)2018{
replace p50p10_cf_marf=p50p10_cf_marf_`i' if aar==`i' 
}

frame change default


drop weights


*obtain weights - education

*numerator 1
mat educ_num_1_couples=J(4,4,.) /*The prob. of counterfactual given couple cell x,x*/

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat educ_num_1_couples[`i',`j']=educ_cf_marf_2018[`i',`j']/(educ_cf_marf_2018[`i',`j']+educ_2018[`i',`j'])
	}
}

mat educ_num_1_single_m=J(4,1,.) /*The prob. of counterfactual given single men cell x*/

forvalues i=1(1)4{
	mat educ_num_1_single_m[`i',1]=educ_single_m_cf_marf_2018[`i',1]/(educ_single_m_cf_marf_2018[`i',1]+educ_single_m_2018[`i',1])
}

mat educ_num_1_single_f=J(1,4,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)4{
	mat educ_num_1_single_f[1,`i']=educ_single_f_cf_marf_2018[1,`i']/(educ_single_f_cf_marf_2018[1,`i']+educ_single_f_2018[1,`i'])
}

*denominator 1
mat educ_de_1_couples=J(4,4,.) /*The prob. of factual given couple cell x,x*/

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat educ_de_1_couples[`i',`j']=educ_2018[`i',`j']/(educ_cf_marf_2018[`i',`j']+educ_2018[`i',`j'])
	}
}

mat educ_de_1_single_m=J(4,1,.) /*The prob. of factual given single men cell x*/

forvalues i=1(1)4{
	mat educ_de_1_single_m[`i',1]=educ_single_m_2018[`i',1]/(educ_single_m_cf_marf_2018[`i',1]+educ_single_m_2018[`i',1])
}

mat educ_de_1_single_f=J(1,4,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)4{
	mat educ_de_1_single_f[1,`i']=educ_single_f_2018[1,`i']/(educ_single_f_cf_marf_2018[1,`i']+educ_single_f_2018[1,`i'])
}

*numerator 2 /*The prob. of being in factual sample*/

sca number_factual=educ_2018[1,1]+educ_2018[1,2]+educ_2018[1,3]+educ_2018[1,4] ///
	+educ_2018[2,1]+educ_2018[2,2]+educ_2018[2,3]+educ_2018[2,4] ///
	+educ_2018[3,1]+educ_2018[3,2]+educ_2018[3,3]+educ_2018[3,4] ///
	+educ_2018[4,1]+educ_2018[4,2]+educ_2018[4,3]+educ_2018[4,4] ///
	+educ_single_m_2018[1,1]+educ_single_m_2018[2,1]+educ_single_m_2018[3,1]+educ_single_m_2018[4,1] ///
	+educ_single_f_2018[1,1]+educ_single_f_2018[1,2]+educ_single_f_2018[1,3]+educ_single_f_2018[1,4]
	
sca number_counterfactual=educ_cf_marf_2018[1,1]+educ_cf_marf_2018[1,2]+educ_cf_marf_2018[1,3]+educ_cf_marf_2018[1,4] ///
	+educ_cf_marf_2018[2,1]+educ_cf_marf_2018[2,2]+educ_cf_marf_2018[2,3]+educ_cf_marf_2018[2,4] ///
	+educ_cf_marf_2018[3,1]+educ_cf_marf_2018[3,2]+educ_cf_marf_2018[3,3]+educ_cf_marf_2018[3,4] ///
	+educ_cf_marf_2018[4,1]+educ_cf_marf_2018[4,2]+educ_cf_marf_2018[4,3]+educ_cf_marf_2018[4,4] ///
	+educ_single_m_cf_marf_2018[1,1]+educ_single_m_cf_marf_2018[2,1]+educ_single_m_cf_marf_2018[3,1]+educ_single_m_cf_marf_2018[4,1] ///
	+educ_single_f_cf_marf_2018[1,1]+educ_single_f_cf_marf_2018[1,2]+educ_single_f_cf_marf_2018[1,3]+educ_single_f_cf_marf_2018[1,4]
	
sca educ_num_2=number_factual/(number_factual+number_counterfactual)

*denominator 2 /*The prob. of being in counterfactual sample*/
sca educ_de_2=number_counterfactual/(number_factual+number_counterfactual)


*weights
mat educ_w_couples=J(4,4,.)

forvalues i=1(1)4{
	forvalues j=1(1)4{
		mat educ_w_couples[`i',`j']=(educ_num_1_couples[`i',`j']/educ_de_1_couples[`i',`j'])*(educ_num_2/educ_de_2)
	}
}

mat educ_w_single_m=J(4,1,.)

forvalues i=1(1)4{
		mat educ_w_single_m[`i',1]=(educ_num_1_single_m[`i',1]/educ_de_1_single_m[`i',1])*(educ_num_2/educ_de_2)
}

mat educ_w_single_f=J(1,4,.)

forvalues i=1(1)4{
		mat educ_w_single_f[1,`i']=(educ_num_1_single_f[1,`i']/educ_de_1_single_f[1,`i'])*(educ_num_2/educ_de_2)
}

gen weights=.
forvalues i=1(1)4{
	forvalues j=1(1)4{
replace weights=educ_w_couples[`i',`j'] if educ_male==`i' & educ_female==`j' & relationship==1
	}
}

forvalues i=1(1)4{
	replace weights=educ_w_single_m[`i',1] if relationship==0 & koen=="1" & fined==`i'
}

forvalues i=1(1)4{
	replace weights=educ_w_single_f[1,`i'] if relationship==0 & koen=="2" & fined==`i'
}

replace weights=1 if weights==. /*Then those with missing type are counted same way in factual and counterfactual scenario*/

*Inequality 
forvalues y=2018(1)2018{
qui ineqdeco household_earnings [weight=weights] if aar==`y'

sca gini_cf_marf_educ_`y'=r(gini)
sca p90p50_cf_marf_educ_`y'=r(p90p50)
sca p50p10_cf_marf_educ_`y'=r(p50)/r(p10)
}


frame change results

gen gini_cf_marf_educ=.
forvalues i=2018(1)2018{
replace gini_cf_marf_educ=gini_cf_marf_educ_`i' if aar==`i' 
}

gen p90p50_cf_marf_educ=.
forvalues i=2018(1)2018{
replace p90p50_cf_marf_educ=p90p50_cf_marf_educ_`i' if aar==`i' 
}

gen p50p10_cf_marf_educ=.
forvalues i=2018(1)2018{
replace p50p10_cf_marf_educ=p50p10_cf_marf_educ_`i' if aar==`i' 
}

frame change default



drop weights


*obtain weights - field

*numerator 1
mat field_num_1_couples=J(8,8,.) /*The prob. of counterfactual given couple cell x,x*/

forvalues i=1(1)8{
	forvalues j=1(1)8{
		mat field_num_1_couples[`i',`j']=field_cf_marf_2018[`i',`j']/(field_cf_marf_2018[`i',`j']+field_2018[`i',`j'])
	}
}

mat field_num_1_single_m=J(8,1,.) /*The prob. of counterfactual given single men cell x*/

forvalues i=1(1)8{
	mat field_num_1_single_m[`i',1]=field_single_m_cf_marf_2018[`i',1]/(field_single_m_cf_marf_2018[`i',1]+field_single_m_2018[`i',1])
}

mat field_num_1_single_f=J(1,8,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)8{
	mat field_num_1_single_f[1,`i']=field_single_f_cf_marf_2018[1,`i']/(field_single_f_cf_marf_2018[1,`i']+field_single_f_2018[1,`i'])
}

*denominator 1
mat field_de_1_couples=J(8,8,.) /*The prob. of factual given couple cell x,x*/

forvalues i=1(1)8{
	forvalues j=1(1)8{
		mat field_de_1_couples[`i',`j']=field_2018[`i',`j']/(field_cf_marf_2018[`i',`j']+field_2018[`i',`j'])
	}
}

mat field_de_1_single_m=J(8,1,.) /*The prob. of factual given single men cell x*/

forvalues i=1(1)8{
	mat field_de_1_single_m[`i',1]=field_single_m_2018[`i',1]/(field_single_m_cf_marf_2018[`i',1]+field_single_m_2018[`i',1])
}

mat field_de_1_single_f=J(1,8,.) /*The prob. of counterfactual given single women cell x*/

forvalues i=1(1)8{
	mat field_de_1_single_f[1,`i']=field_single_f_2018[1,`i']/(field_single_f_cf_marf_2018[1,`i']+field_single_f_2018[1,`i'])
}

*numerator 2 /*The prob. of being in factual sample*/

sca number_factual=field_2018[1,1]+field_2018[1,2]+field_2018[1,3]+field_2018[1,4]+field_2018[1,5]+field_2018[1,6]+field_2018[1,7]+field_2018[1,8] ///
	+field_2018[2,1]+field_2018[2,2]+field_2018[2,3]+field_2018[2,4]+field_2018[2,5]+field_2018[2,6]+field_2018[2,7]+field_2018[2,8] ///
	+field_2018[3,1]+field_2018[3,2]+field_2018[3,3]+field_2018[3,4]+field_2018[3,5]+field_2018[3,6]+field_2018[3,7]+field_2018[3,8] ///
	+field_2018[4,1]+field_2018[4,2]+field_2018[4,3]+field_2018[4,4]+field_2018[4,5]+field_2018[4,6]+field_2018[4,7]+field_2018[4,8] ///
	+field_2018[5,1]+field_2018[5,2]+field_2018[5,3]+field_2018[5,4]+field_2018[5,5]+field_2018[5,6]+field_2018[5,7]+field_2018[5,8] ///
	+field_2018[6,1]+field_2018[6,2]+field_2018[6,3]+field_2018[6,4]+field_2018[6,5]+field_2018[6,6]+field_2018[6,7]+field_2018[6,8] ///
	+field_2018[7,1]+field_2018[7,2]+field_2018[7,3]+field_2018[7,4]+field_2018[7,5]+field_2018[7,6]+field_2018[7,7]+field_2018[7,8] ///
	+field_2018[8,1]+field_2018[8,2]+field_2018[8,3]+field_2018[8,4]+field_2018[8,5]+field_2018[8,6]+field_2018[8,7]+field_2018[8,8] ///
	+field_single_m_2018[1,1]+field_single_m_2018[2,1]+field_single_m_2018[3,1]+field_single_m_2018[4,1]+field_single_m_2018[5,1]+field_single_m_2018[6,1]+field_single_m_2018[7,1]+field_single_m_2018[8,1] ///
	+field_single_f_2018[1,1]+field_single_f_2018[1,2]+field_single_f_2018[1,3]+field_single_f_2018[1,4]+field_single_f_2018[1,5]+field_single_f_2018[1,6]+field_single_f_2018[1,7]+field_single_f_2018[1,8]
	
sca number_factual=field_cf_marf_2018[1,1]+field_cf_marf_2018[1,2]+field_cf_marf_2018[1,3]+field_cf_marf_2018[1,4]+field_cf_marf_2018[1,5]+field_cf_marf_2018[1,6]+field_cf_marf_2018[1,7]+field_cf_marf_2018[1,8] ///
	+field_cf_marf_2018[2,1]+field_cf_marf_2018[2,2]+field_cf_marf_2018[2,3]+field_cf_marf_2018[2,4]+field_cf_marf_2018[2,5]+field_cf_marf_2018[2,6]+field_cf_marf_2018[2,7]+field_cf_marf_2018[2,8] ///
	+field_cf_marf_2018[3,1]+field_cf_marf_2018[3,2]+field_cf_marf_2018[3,3]+field_cf_marf_2018[3,4]+field_cf_marf_2018[3,5]+field_cf_marf_2018[3,6]+field_cf_marf_2018[3,7]+field_cf_marf_2018[3,8] ///
	+field_cf_marf_2018[4,1]+field_cf_marf_2018[4,2]+field_cf_marf_2018[4,3]+field_cf_marf_2018[4,4]+field_cf_marf_2018[4,5]+field_cf_marf_2018[4,6]+field_cf_marf_2018[4,7]+field_cf_marf_2018[4,8] ///
	+field_cf_marf_2018[5,1]+field_cf_marf_2018[5,2]+field_cf_marf_2018[5,3]+field_cf_marf_2018[5,4]+field_cf_marf_2018[5,5]+field_cf_marf_2018[5,6]+field_cf_marf_2018[5,7]+field_cf_marf_2018[5,8] ///
	+field_cf_marf_2018[6,1]+field_cf_marf_2018[6,2]+field_cf_marf_2018[6,3]+field_cf_marf_2018[6,4]+field_cf_marf_2018[6,5]+field_cf_marf_2018[6,6]+field_cf_marf_2018[6,7]+field_cf_marf_2018[6,8] ///
	+field_cf_marf_2018[7,1]+field_cf_marf_2018[7,2]+field_cf_marf_2018[7,3]+field_cf_marf_2018[7,4]+field_cf_marf_2018[7,5]+field_cf_marf_2018[7,6]+field_cf_marf_2018[7,7]+field_cf_marf_2018[7,8] ///
	+field_cf_marf_2018[8,1]+field_cf_marf_2018[8,2]+field_cf_marf_2018[8,3]+field_cf_marf_2018[8,4]+field_cf_marf_2018[8,5]+field_cf_marf_2018[8,6]+field_cf_marf_2018[8,7]+field_cf_marf_2018[8,8] ///
	+field_single_m_cf_marf_2018[1,1]+field_single_m_cf_marf_2018[2,1]+field_single_m_cf_marf_2018[3,1]+field_single_m_cf_marf_2018[4,1]+field_single_m_cf_marf_2018[5,1]+field_single_m_cf_marf_2018[6,1]+field_single_m_cf_marf_2018[7,1]+field_single_m_cf_marf_2018[8,1] ///
	+field_single_f_cf_marf_2018[1,1]+field_single_f_cf_marf_2018[1,2]+field_single_f_cf_marf_2018[1,3]+field_single_f_cf_marf_2018[1,4]+field_single_f_cf_marf_2018[1,5]+field_single_f_cf_marf_2018[1,6]+field_single_f_cf_marf_2018[1,7]+field_single_f_cf_marf_2018[1,8]

	
sca field_num_2=number_factual/(number_factual+number_counterfactual)

*denominator 2 /*The prob. of being in counterfactual sample*/
sca field_de_2=number_counterfactual/(number_factual+number_counterfactual)


*weights
mat field_w_couples=J(8,8,.)

forvalues i=1(1)8{
	forvalues j=1(1)8{
		mat field_w_couples[`i',`j']=(field_num_1_couples[`i',`j']/field_de_1_couples[`i',`j'])*(field_num_2/field_de_2)
	}
}

mat field_w_single_m=J(8,1,.)

forvalues i=1(1)8{
		mat field_w_single_m[`i',1]=(field_num_1_single_m[`i',1]/field_de_1_single_m[`i',1])*(field_num_2/field_de_2)
}

mat field_w_single_f=J(1,8,.)

forvalues i=1(1)8{
		mat field_w_single_f[1,`i']=(field_num_1_single_f[1,`i']/field_de_1_single_f[1,`i'])*(field_num_2/field_de_2)
}

gen weights=.
forvalues i=1(1)8{
	forvalues j=1(1)8{
replace weights=field_w_couples[`i',`j'] if field_male==`i' & field_female==`j' & relationship==1
	}
}

forvalues i=1(1)8{
	replace weights=field_w_single_m[`i',1] if relationship==0 & koen=="1" & educ_level_field_num==`i'
}

forvalues i=1(1)8{
	replace weights=field_w_single_f[1,`i'] if relationship==0 & koen=="2" & educ_level_field_num==`i'
}

replace weights=1 if weights==. /*Then those with missing type are counted same way in factual and counterfactual scenario*/

*Inequality 
forvalues y=2018(1)2018{
qui ineqdeco household_earnings [weight=weights] if aar==`y'

sca gini_cf_marf_field_`y'=r(gini)
sca p90p50_cf_marf_field_`y'=r(p90p50)
sca p50p10_cf_marf_field_`y'=r(p50)/r(p10)
}


frame change results

gen gini_cf_marf_field=.
forvalues i=2018(1)2018{
replace gini_cf_marf_field=gini_cf_marf_field_`i' if aar==`i' 
}

gen p90p50_cf_marf_field=.
forvalues i=2018(1)2018{
replace p90p50_cf_marf_field=p90p50_cf_marf_field_`i' if aar==`i' 
}

gen p50p10_cf_marf_field=.
forvalues i=2018(1)2018{
replace p50p10_cf_marf_field=p50p10_cf_marf_field_`i' if aar==`i' 
}

frame change default



drop weights

}


*SAVE THE RESULTS*
frame change results

save "Results\tab_B2\results.dta", replace

clear
